I'm trialing Rollup and its plugins.
My attempts to replace the string ./build/
in a manifest created by rollup-plugin-hash have no effect whatsoever. More specific, this doesn't replace anything. :
replace({
include: [jsManifest],
exclude: 'node_modules/**',
delimiters: ['',''],
// "./build/": JSON.stringify(''),
values: {
"./build/": JSON.stringify(''),
'./build/': JSON.stringify(''),
"./build/": JSON.stringify(""),
'./build/': JSON.stringify(""),
"./build/": '',
'./build/': '',
"./build/": "",
'./build/': "",
build: '',
build: "",
}
}),
This is the content of the manifest file ~/dev/web/rollup-with-browsersync-hash-replace/build/main.js-manifest.json
:
{"./build/js/main.min.js":"./build/js/main.9bfe24db2cad759965e13900c7c5e016.min.js"}
Further, after manually removing ./build/
from the manifest file so that it becomes this, :
{"js/main.min.js":"js/main.9bfe24db2cad759965e13900c7c5e016.min.js"}
the subsequent replace also has no effect.
More specific this one :
replace({
include: [dist + '/**/*.html'],
exclude: 'node_modules/**',
values: getJsonFile(jsManifest),
}),
This is the content of the index.html
file :
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width,minimum-scale=1,initial-scale=1">
<title>Testing Rollup</title>
</head>
<body>
<h1>Testing Rollup</h1>
<p>
Testing <a href="http://rollupjs.org/">Rollup</a>.
</p>
<!-- JS bundle generated by rollup.js -->
<script src="js/main.min.js"></script>
</body>
</html>
This is the content of my rollup.config.js
file :
// Rollup plugins
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 { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import browsersync from 'rollup-plugin-browsersync'
import hash from 'rollup-plugin-hash';
// PostCSS plugins
import simplevars from 'postcss-simple-vars';
import nested from 'postcss-nested';
import presetEnv from 'postcss-preset-env';
import cssnano from 'cssnano';
import { readFileSync } from 'fs-extra';
const src = './src', dist = './build', scriptsSrc = src + '/scripts', scriptsDest = dist + '/js', stylesSrc = src + '/styles', stylesDest = dist + '/css',
jsManifest = dist + '/main.js-manifest.json';
const getJsonFile = (file) => {
try {
return JSON.parse(readFileSync(file, 'utf8'));
} catch (err) {
if (err.code === 'ENOENT') {
console.log('Could not find file : ' + file);
return {};
}
throw err;
}
};
export default {
input: scriptsSrc + '/main.js',
output: {
file: scriptsDest + '/main.min.js',
format: 'iife',
sourcemap: 'inline',
},
watch: {
include: src + '/**',
exclude: 'node_modules/**',
clearScreen: false,
},
plugins: [
postcss({
plugins: [
simplevars(),
nested(),
presetEnv(),
cssnano(),
],
extensions: [ '.css' ],
}),
resolve({
jsnext: true,
main: true,
browser: true,
}),
// Note that rollup-plugin-commonjs should go before other plugins that transform your modules
// — this is to prevent other plugins from making changes that break the CommonJS detection.
commonjs(),
eslint({
exclude: [
stylesSrc + '/**',
]
}),
babel({
exclude: 'node_modules/**',
}),
replace({
exclude: 'node_modules/**',
ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
}),
hash({
dest: scriptsDest + '/main.[hash:32].min.js',
replace:true,
algorithm: 'sha512',
manifest: jsManifest,
}),
replace({
include: [jsManifest],
exclude: 'node_modules/**',
delimiters: ['',''],
// "./build/": JSON.stringify(''),
values: {
"./build/": JSON.stringify(''),
'./build/': JSON.stringify(''),
"./build/": JSON.stringify(""),
'./build/': JSON.stringify(""),
"./build/": '',
'./build/': '',
"./build/": "",
'./build/': "",
build: '',
build: "",
}
}),
replace({
include: [dist + '/**/*.html'],
exclude: 'node_modules/**',
values: getJsonFile(jsManifest),
}),
(process.env.NODE_ENV === 'production' && terser()),
browsersync({
files: dist + '/**',
watch: true,
ignore: 'node_modules/**',
server: dist + '/',
logPrefix: 'ROLLUP',
reloadDelay: 0,
reloadDebounce: 0,
reloadThrottle: 0,
injectChanges: true,
}),
],
};