我懷疑我的問題在於我的文件樹的結構,但我沒有太多的控制權,因爲我在使用WordPress主題。RollupJS插件沒有解決
我的rollup.config.js
有幾個插件選項無法使用。我改變了導入語句的順序,插件順序,改變路徑,但結果是一樣的,儘管配置文件本身似乎被處理。
我通過shell腳本從mytheme/assets/js
運行> rollup -c
。這是因爲我們可以爲每個主題單獨構建,同時共享一個共同的node_modules
。如果這是一個愚蠢的做法,請給我上學。
我在遇到幾個問題時遇到了問題。
node_modules
應該被默認忽略,但我看到皮棉誤差爲node_modules\babel-polyfill\dist\polyfill.js
和node_modules\debug\src\browser.js
。我試圖明確排除node_modules/**
和eslint()
條目rollup.config.js
中的文件名本身,但它沒有區別。path\to\node_modules\babel-polyfill\dist\polyfill.js 3:568 error 'exports' is defined but never used no-unused-vars ...
的
replace
插件未更換ENV
常數main.es6.js
:5:6 error 'ENV' is not defined no-undef
這裏是我的文件樹:
.
├── .babelrc
├── .eslintrc
├── .gitignore
├── package.json
├── package-lock.json
├── node_modules/
├── web/
├── wp-content/
├── themes/
├── mytheme
├── assets
├── js
├── rollup.config.js
├── build/
├── main.js (build file)
├── src
├── main.es6.js (input file)
這是我的shell腳本,只要修改了*.es6.js
文件就會運行。
#!/bin/sh
server="$1/web/wp-content"
target_paths="$server/lib $server/themes $server/plugins"
child_path="/*/assets/js/rollup.config.js"
js_dirs=$(/bin/find $target_paths -type f -path $child_path)
echo "paths: $target_paths"
echo "config_files: $js_dirs"
for js_dir in $js_dirs
do
# parent directory (assets/js)
js_dir=$(dirname "$js_dir")
# location of main.es6.js (assets/js/src)
src_dir="$js_dir/src"
# output directory (assets/js/build)
build_dir="$js_dir/build"
echo ""
# cd $js_dir && rollup -c
cd $js_dir && NODE_ENV=production rollup -c
done
這裏是main.es6.js
'use strict';
import 'babel-polyfill/dist/polyfill';
import debug from 'debug';
if (ENV !== 'production') {
debug.enable('*');
const log = debug('app:log');
log('Debug logging enabled.');
} else {
debug.disable();
}
第幾行這裏是我的rollup.config.js
import babel from 'rollup-plugin-babel';
import eslint from 'rollup-plugin-eslint';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import uglify from 'rollup-plugin-uglify';
export default {
entry: 'src/main.es6.js',
format: 'iife',
dest: 'build/main.js',
sourceMap: 'inline',
plugins: [
resolve({
jsnext: true,
main: true,
browser: true
}),
commonjs(), // to play nice with require(), which is not ES6+
eslint(),
babel({
exclude: ['**/assets/js', 'node_modules/**']
}),
replace({
exclude: 'node_modules/**',
ENV: JSON.stringify(process.env.NODE_ENV || 'dev')
}),
uglify()
//(process.env.NODE_ENV === 'production' && uglify())
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
if (warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1) {
return;
}
// console.warn everything else
console.warn(warning.message);
}
}
下面的內容是我.eslintrc
{
"env": {
"browser": true,
"es6": true
},
"globals": {
"jQuery": true,
"$": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [ 1, 2 ],
"quotes": [ 0, "single" ],
"linebreak-style": [ 0, "windows" ],
"semi": [ 0, "always" ]
}
}
這是我.babelrc
{
"presets": [
[
"env",
{
"modules": false,
"targets": {
"browsers": ["last 2 versions", "ie > 10"]
},
"useBuiltIns": true
}
]
],
"plugins": [
"external-helpers"
]
}
感謝您的建議,Rich。我將不得不考慮一個rollup.config是否適用於我們,這很有趣,並且可以節省大量的跳躍。 但是,'path.resolve()'建議返回了一個錯誤:'錯誤:路徑未定義。 'path'從哪裏來的,這是一個NodeJS對象嗎? 同樣,交換'replace'和'eslint'插件對'ENV'錯誤沒有影響。 如果不是很清楚,我應該補充說我是一個完整的Node newb。 – lancemonotone
您需要在Rollup配置的頂部添加'path''的導入路徑。嗯,不知道ENV的事情。 (https://gist.github.com/Rich-Harris/88c5fc2ac6dc941b22e7996af05d70ff)我很害怕 –
導入'path'會清除錯誤,但會排除:'''{{path。 resolve('node_modules')}/**']'仍然包含polyfill。我想知道是否它是由我的'main.es6.js'顯式導入的,而不是構建過程某些部分的依賴。另外,我認爲全局'rollup.config'可能適合我們。我在接下來的兩週里正在休假,所以我現在不得不離開這裏。感謝您的幫助。 – lancemonotone