From 11a2ff83db249aaeb0a2ea31a911b278692839a3 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 22:40:02 -0500 Subject: [PATCH 1/9] refactor: use fs.readFileSync --- build-plugins/generate-license-file.ts | 8 ++++---- cli/run/watch-cli.ts | 6 +++--- rollup.config.ts | 4 ++-- scripts/perf.js | 6 +++--- .../samples/deprecated/emit-asset/_config.js | 4 ++-- .../samples/emit-file/reference-files/_config.js | 4 ++-- test/cli/index.js | 3 ++- test/cli/samples/config-no-output/_config.js | 6 +++--- test/cli/samples/sourcemap-hidden/_config.js | 10 +++++----- test/cli/samples/watch/close-stdin/wrapper.js | 8 ++++---- test/form/index.js | 9 +++++---- .../samples/deprecated/emit-asset/_config.js | 6 +++--- test/form/samples/emit-asset-file/_config.js | 6 +++--- .../emit-file-tree-shaken-access/_config.js | 4 ++-- test/function/samples/custom-loaders/_config.js | 6 +++--- .../custom-path-resolver-on-entry/_config.js | 4 ++-- .../export-default-anonymous-function/_config.js | 4 ++-- .../samples/transform-without-code/_config.js | 4 ++-- .../combined-sourcemap-with-loader/_config.js | 4 ++-- .../samples/excludes-plugin-helpers/_config.js | 4 ++-- test/sourcemaps/samples/loaders/_config.js | 4 ++-- .../samples/reified-namespace/_config.js | 4 ++-- .../samples/single-length-segments/_config.js | 8 ++++---- test/watch/index.js | 16 ++++++---------- 24 files changed, 70 insertions(+), 72 deletions(-) diff --git a/build-plugins/generate-license-file.ts b/build-plugins/generate-license-file.ts index e64391c9617..42f1e363c38 100644 --- a/build-plugins/generate-license-file.ts +++ b/build-plugins/generate-license-file.ts @@ -1,9 +1,9 @@ -import fs from 'fs'; +import { readFileSync, writeFileSync } from 'fs'; import { PluginImpl } from 'rollup'; import license, { Dependency, Person } from 'rollup-plugin-license'; function generateLicenseFile(dependencies: Dependency[]) { - const coreLicense = fs.readFileSync('LICENSE-CORE.md'); + const coreLicense = readFileSync('LICENSE-CORE.md'); const licenses = new Set(); const dependencyLicenseTexts = dependencies .sort(({ name: nameA }, { name: nameB }) => (nameA! > nameB! ? 1 : -1)) @@ -52,9 +52,9 @@ function generateLicenseFile(dependencies: Dependency[]) { `${Array.from(licenses).join(', ')}\n\n` + `# Bundled dependencies:\n` + dependencyLicenseTexts; - const existingLicenseText = fs.readFileSync('LICENSE.md', 'utf8'); + const existingLicenseText = readFileSync('LICENSE.md', 'utf8'); if (existingLicenseText !== licenseText) { - fs.writeFileSync('LICENSE.md', licenseText); + writeFileSync('LICENSE.md', licenseText); console.warn('LICENSE.md updated. You should commit the updated file.'); } } diff --git a/cli/run/watch-cli.ts b/cli/run/watch-cli.ts index 7baa555d876..3beee5d2027 100644 --- a/cli/run/watch-cli.ts +++ b/cli/run/watch-cli.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { FSWatcher, readFileSync } from 'fs'; import chokidar from 'chokidar'; import dateTime from 'date-time'; import ms from 'pretty-ms'; @@ -22,7 +22,7 @@ export async function watch(command: Record): Promise { let configs: MergedRollupOptions[]; let warnings: BatchWarnings; let watcher: RollupWatcher; - let configWatcher: fs.FSWatcher; + let configWatcher: FSWatcher; const configFile = command.config ? getConfigPath(command.config) : null; onExit(close); @@ -42,7 +42,7 @@ export async function watch(command: Record): Promise { async function reloadConfigFile() { try { - const newConfigFileData = fs.readFileSync(configFile, 'utf-8'); + const newConfigFileData = readFileSync(configFile, 'utf-8'); if (newConfigFileData === configFileData) { return; } diff --git a/rollup.config.ts b/rollup.config.ts index 3273078b6bf..cebab7ed828 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import { readFileSync } from 'fs'; import path from 'path'; import alias from '@rollup/plugin-alias'; import commonjs from '@rollup/plugin-commonjs'; @@ -18,7 +18,7 @@ import pkg from './package.json'; const commitHash = (function () { try { - return fs.readFileSync('.commithash', 'utf-8'); + return readFileSync('.commithash', 'utf-8'); } catch { return 'unknown'; } diff --git a/scripts/perf.js b/scripts/perf.js index 0ccbeaacd25..be74ae5c56f 100644 --- a/scripts/perf.js +++ b/scripts/perf.js @@ -1,6 +1,6 @@ /* global gc */ -const fs = require('fs'); +const { readFileSync, writeFileSync } = require('fs'); const path = require('path'); const colorette = require('colorette'); const prettyBytes = require('pretty-bytes'); @@ -137,7 +137,7 @@ function clearLines(numberOfLines) { function getExistingTimings() { try { - const timings = JSON.parse(fs.readFileSync(perfFile, 'utf8')); + const timings = JSON.parse(readFileSync(perfFile, 'utf8')); console.info( colorette.bold( `Comparing with ${colorette.cyan(perfFile)}. Delete this file to create a new base line.` @@ -151,7 +151,7 @@ function getExistingTimings() { function persistTimings(timings) { try { - fs.writeFileSync(perfFile, JSON.stringify(timings, null, 2), 'utf8'); + writeFileSync(perfFile, JSON.stringify(timings, null, 2), 'utf8'); console.info( colorette.bold( `Saving performance information to new reference file ${colorette.cyan(perfFile)}.` diff --git a/test/chunking-form/samples/deprecated/emit-asset/_config.js b/test/chunking-form/samples/deprecated/emit-asset/_config.js index dad3aeeaf09..f9625ac7974 100644 --- a/test/chunking-form/samples/deprecated/emit-asset/_config.js +++ b/test/chunking-form/samples/deprecated/emit-asset/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -21,7 +21,7 @@ module.exports = { if (id.endsWith('.svg')) { return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset( path.basename(id), - fs.readFileSync(id) + readFileSync(id) )};`; } } diff --git a/test/chunking-form/samples/emit-file/reference-files/_config.js b/test/chunking-form/samples/emit-file/reference-files/_config.js index 57ba1d3de55..6569204399f 100644 --- a/test/chunking-form/samples/emit-file/reference-files/_config.js +++ b/test/chunking-form/samples/emit-file/reference-files/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -21,7 +21,7 @@ module.exports = { return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({ type: 'asset', name: path.basename(id), - source: fs.readFileSync(id) + source: readFileSync(id) })};`; } } diff --git a/test/cli/index.js b/test/cli/index.js index 5401bf699a0..963f3d03fb5 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -1,5 +1,6 @@ const assert = require('assert'); const { exec } = require('child_process'); +const { readFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const { @@ -109,7 +110,7 @@ runTestSuiteWithSamples( done(err); } } else { - const expected = sander.readFileSync('_expected.js').toString(); + const expected = readFileSync('_expected.js', 'utf8'); try { assert.equal(normaliseOutput(code), normaliseOutput(expected)); done(); diff --git a/test/cli/samples/config-no-output/_config.js b/test/cli/samples/config-no-output/_config.js index 148b688c86b..1f0696157c2 100644 --- a/test/cli/samples/config-no-output/_config.js +++ b/test/cli/samples/config-no-output/_config.js @@ -1,12 +1,12 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync, unlinkSync } = require('fs'); module.exports = { description: 'uses -o from CLI', command: 'rollup -c -o output.js', test() { - const output = fs.readFileSync('output.js', 'utf-8'); + const output = readFileSync('output.js', 'utf-8'); assert.equal(output.trim(), 'console.log(42);'); - fs.unlinkSync('output.js'); + unlinkSync('output.js'); } }; diff --git a/test/cli/samples/sourcemap-hidden/_config.js b/test/cli/samples/sourcemap-hidden/_config.js index 6bd4d61f101..5dd0871d5a0 100644 --- a/test/cli/samples/sourcemap-hidden/_config.js +++ b/test/cli/samples/sourcemap-hidden/_config.js @@ -1,16 +1,16 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync, unlinkSync } = require('fs'); module.exports = { description: 'omits sourcemap comments', command: 'rollup -i main.js -f es -m hidden -o output.js', test() { - assert.equal(fs.readFileSync('output.js', 'utf-8').trim(), 'console.log( 42 );'); - fs.unlinkSync('output.js'); + assert.equal(readFileSync('output.js', 'utf-8').trim(), 'console.log( 42 );'); + unlinkSync('output.js'); assert.equal( - fs.readFileSync('output.js.map', 'utf-8').trim(), + readFileSync('output.js.map', 'utf-8').trim(), '{"version":3,"file":"output.js","sources":["main.js"],"sourcesContent":["console.log( 42 );\\n"],"names":[],"mappings":"AAAA,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE"}' ); - fs.unlinkSync('output.js.map'); + unlinkSync('output.js.map'); } }; diff --git a/test/cli/samples/watch/close-stdin/wrapper.js b/test/cli/samples/watch/close-stdin/wrapper.js index a978de79c4b..32506153c5d 100755 --- a/test/cli/samples/watch/close-stdin/wrapper.js +++ b/test/cli/samples/watch/close-stdin/wrapper.js @@ -1,7 +1,7 @@ #!/usr/bin/env node const stream = require('stream'); -const fs = require('fs'); +const { mkdirSync, readFileSync, writeFileSync } = require('fs'); const chokidar = require('chokidar'); const path = require('path'); @@ -14,13 +14,13 @@ process.stdin = new stream.Readable({ }); const outputDir = path.resolve(__dirname, '_actual'); -fs.mkdirSync(outputDir); +mkdirSync(outputDir); const outputFile = path.resolve(outputDir, 'out.js'); const INITIAL_OUTPUT = 'NOT WRITTEN'; -fs.writeFileSync(outputFile, INITIAL_OUTPUT); +writeFileSync(outputFile, INITIAL_OUTPUT); const watcher = chokidar.watch(outputFile).on('change', () => { - if (fs.readFileSync(outputFile, 'utf8') !== INITIAL_OUTPUT) { + if (readFileSync(outputFile, 'utf8') !== INITIAL_OUTPUT) { watcher.close(); // This closes stdin process.stdin.push(null); diff --git a/test/form/index.js b/test/form/index.js index 928a9cdd641..97c1e752308 100644 --- a/test/form/index.js +++ b/test/form/index.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const { readFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -79,19 +80,19 @@ runTestSuiteWithSamples('form', path.resolve(__dirname, 'samples'), (dir, config async function generateAndTestBundle(bundle, outputOptions, expectedFile, { show }) { await bundle.write(outputOptions); - const actualCode = normaliseOutput(sander.readFileSync(outputOptions.file)); + const actualCode = normaliseOutput(readFileSync(outputOptions.file, 'utf8')); let expectedCode; let actualMap; let expectedMap; try { - expectedCode = normaliseOutput(sander.readFileSync(expectedFile)); + expectedCode = normaliseOutput(readFileSync(expectedFile, 'utf8')); } catch (err) { expectedCode = 'missing file'; } try { - actualMap = JSON.parse(sander.readFileSync(outputOptions.file + '.map').toString()); + actualMap = JSON.parse(readFileSync(outputOptions.file + '.map', 'utf8')); actualMap.sourcesContent = actualMap.sourcesContent ? actualMap.sourcesContent.map(normaliseOutput) : null; @@ -100,7 +101,7 @@ async function generateAndTestBundle(bundle, outputOptions, expectedFile, { show } try { - expectedMap = JSON.parse(sander.readFileSync(expectedFile + '.map').toString()); + expectedMap = JSON.parse(readFileSync(expectedFile + '.map', 'utf8')); expectedMap.sourcesContent = actualMap.sourcesContent ? expectedMap.sourcesContent.map(normaliseOutput) : null; diff --git a/test/form/samples/deprecated/emit-asset/_config.js b/test/form/samples/deprecated/emit-asset/_config.js index 6d4c88ac239..f7930b32270 100644 --- a/test/form/samples/deprecated/emit-asset/_config.js +++ b/test/form/samples/deprecated/emit-asset/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -17,7 +17,7 @@ module.exports = { if (id.endsWith('.svg')) { return `export default import.meta.ROLLUP_ASSET_URL_${this.emitAsset( path.basename(id), - fs.readFileSync(id) + readFileSync(id) )};`; } }, @@ -30,7 +30,7 @@ module.exports = { assert.strictEqual(asset.isAsset, true); assert.strictEqual(asset.type, 'asset'); assert.ok( - asset.source.equals(fs.readFileSync(path.resolve(__dirname, 'logo.svg'))), + asset.source.equals(readFileSync(path.resolve(__dirname, 'logo.svg'))), 'asset has correct source' ); assert.ok(keys[1].endsWith('.js'), `${keys[1]} ends with ".js"`); diff --git a/test/form/samples/emit-asset-file/_config.js b/test/form/samples/emit-asset-file/_config.js index 5976fabd374..b4bbd8926d7 100644 --- a/test/form/samples/emit-asset-file/_config.js +++ b/test/form/samples/emit-asset-file/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -16,7 +16,7 @@ module.exports = { return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({ type: 'asset', name: path.basename(id), - source: fs.readFileSync(id) + source: readFileSync(id) })};`; } }, @@ -28,7 +28,7 @@ module.exports = { assert.strictEqual(asset.fileName, 'assets/logo-25585ac1.svg'); assert.strictEqual(asset.type, 'asset'); assert.ok( - asset.source.equals(fs.readFileSync(path.resolve(__dirname, 'logo.svg'))), + asset.source.equals(readFileSync(path.resolve(__dirname, 'logo.svg'))), 'asset has correct source' ); assert.ok(keys[1].endsWith('.js'), `${keys[1]} ends with ".js"`); diff --git a/test/form/samples/emit-file-tree-shaken-access/_config.js b/test/form/samples/emit-file-tree-shaken-access/_config.js index 1feae6664f4..3b52af0c675 100644 --- a/test/form/samples/emit-file-tree-shaken-access/_config.js +++ b/test/form/samples/emit-file-tree-shaken-access/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -15,7 +15,7 @@ module.exports = { return `export default import.meta.ROLLUP_FILE_URL_${this.emitFile({ type: 'asset', name: path.basename(id), - source: fs.readFileSync(id) + source: readFileSync(id) })};`; } } diff --git a/test/function/samples/custom-loaders/_config.js b/test/function/samples/custom-loaders/_config.js index e2f0cec12b1..e9e52b0b884 100644 --- a/test/function/samples/custom-loaders/_config.js +++ b/test/function/samples/custom-loaders/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { readFileSync } = require('fs'); module.exports = { description: 'uses custom loaders, falling back to default', @@ -7,14 +7,14 @@ module.exports = { { load(id) { if (/foo\.js/.test(id)) { - return fs.readFileSync(id, 'utf-8').replace('@', 1); + return readFileSync(id, 'utf-8').replace('@', 1); } } }, { load(id) { if (/bar\.js/.test(id)) { - return fs.readFileSync(id, 'utf-8').replace('@', 2); + return readFileSync(id, 'utf-8').replace('@', 2); } } } diff --git a/test/function/samples/custom-path-resolver-on-entry/_config.js b/test/function/samples/custom-path-resolver-on-entry/_config.js index 7d49020fd95..c92be833424 100644 --- a/test/function/samples/custom-path-resolver-on-entry/_config.js +++ b/test/function/samples/custom-path-resolver-on-entry/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); const cachedModules = { @@ -25,7 +25,7 @@ module.exports = { return cachedModules[moduleId]; } - return fs.readFileSync(moduleId, 'utf-8'); + return readFileSync(moduleId, 'utf-8'); } } ] diff --git a/test/function/samples/export-default-anonymous-function/_config.js b/test/function/samples/export-default-anonymous-function/_config.js index 030af6269a7..14a8ec085be 100644 --- a/test/function/samples/export-default-anonymous-function/_config.js +++ b/test/function/samples/export-default-anonymous-function/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); module.exports = { @@ -10,7 +10,7 @@ module.exports = { return path.basename(importee).replace(/\..+/, ''); }, load(id) { - return fs.readFileSync(path.join(__dirname, id + '.js'), 'utf-8'); + return readFileSync(path.join(__dirname, id + '.js'), 'utf-8'); } } ] diff --git a/test/function/samples/transform-without-code/_config.js b/test/function/samples/transform-without-code/_config.js index dc9d1447d13..cfe8d21e839 100644 --- a/test/function/samples/transform-without-code/_config.js +++ b/test/function/samples/transform-without-code/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); const { SourceMapConsumer } = require('source-map'); @@ -21,7 +21,7 @@ module.exports = { const smc = await new SourceMapConsumer(map); const originalLoc = smc.originalPositionFor({ line, column }); assert.notStrictEqual(originalLoc.line, null); - const originalCode = fs.readFileSync(path.join(__dirname, 'main.js'), 'utf8'); + const originalCode = readFileSync(path.join(__dirname, 'main.js'), 'utf8'); assert.strictEqual( originalCode.split('\n')[originalLoc.line - 1].substr(originalLoc.column, 2), '42' diff --git a/test/sourcemaps/samples/combined-sourcemap-with-loader/_config.js b/test/sourcemaps/samples/combined-sourcemap-with-loader/_config.js index 1f670313146..c9d16748ce3 100644 --- a/test/sourcemaps/samples/combined-sourcemap-with-loader/_config.js +++ b/test/sourcemaps/samples/combined-sourcemap-with-loader/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const buble = require('buble'); const MagicString = require('magic-string'); const { SourceMapConsumer } = require('source-map'); @@ -11,7 +11,7 @@ module.exports = { plugins: [ { load(id) { - const code = fs.readFileSync(id, 'utf-8'); + const code = readFileSync(id, 'utf-8'); const out = buble.transform(code, { transforms: { modules: false }, sourceMap: true, diff --git a/test/sourcemaps/samples/excludes-plugin-helpers/_config.js b/test/sourcemaps/samples/excludes-plugin-helpers/_config.js index 393c9e825a3..e64a91e702f 100644 --- a/test/sourcemaps/samples/excludes-plugin-helpers/_config.js +++ b/test/sourcemaps/samples/excludes-plugin-helpers/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); const HELPER = '\0helper'; @@ -18,7 +18,7 @@ module.exports = { load(id) { if (id === HELPER) { - return fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8'); + return readFileSync(path.join(__dirname, 'helper.js'), 'utf-8'); } } } diff --git a/test/sourcemaps/samples/loaders/_config.js b/test/sourcemaps/samples/loaders/_config.js index 9e45fa58ec9..7df0a3b4fef 100644 --- a/test/sourcemaps/samples/loaders/_config.js +++ b/test/sourcemaps/samples/loaders/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const buble = require('buble'); const { SourceMapConsumer } = require('source-map'); const getLocation = require('../../getLocation'); @@ -16,7 +16,7 @@ module.exports = { id = id.replace(/bar.js$/, 'foo.js'); } - const code = fs.readFileSync(id, 'utf-8'); + const code = readFileSync(id, 'utf-8'); const out = buble.transform(code, { transforms: { modules: false }, diff --git a/test/sourcemaps/samples/reified-namespace/_config.js b/test/sourcemaps/samples/reified-namespace/_config.js index 431758d276a..09fd70e39f5 100644 --- a/test/sourcemaps/samples/reified-namespace/_config.js +++ b/test/sourcemaps/samples/reified-namespace/_config.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); const { SourceMapConsumer } = require('source-map'); const getLocation = require('../../getLocation'); @@ -9,7 +9,7 @@ module.exports = { async test(code, map) { const smc = await new SourceMapConsumer(map); - const main = fs.readFileSync(path.join(__dirname, 'main.js'), 'utf-8'); + const main = readFileSync(path.join(__dirname, 'main.js'), 'utf-8'); const generatedLoc = getLocation(code, 'deepEqual'); const actual = smc.originalPositionFor(generatedLoc); diff --git a/test/sourcemaps/samples/single-length-segments/_config.js b/test/sourcemaps/samples/single-length-segments/_config.js index 1b38deb956d..7850b91ad7a 100644 --- a/test/sourcemaps/samples/single-length-segments/_config.js +++ b/test/sourcemaps/samples/single-length-segments/_config.js @@ -1,10 +1,10 @@ const assert = require('assert'); -const fs = require('fs'); +const { readFileSync } = require('fs'); const path = require('path'); const { SourceMapConsumer } = require('source-map'); const getLocation = require('../../getLocation'); -const original = fs.readFileSync(path.resolve(__dirname, 'main.js'), 'utf-8'); +const original = readFileSync(path.resolve(__dirname, 'main.js'), 'utf-8'); module.exports = { description: 'handles single-length sourcemap segments', @@ -13,8 +13,8 @@ module.exports = { { transform() { return { - code: fs.readFileSync(path.resolve(__dirname, 'output.js'), 'utf-8'), - map: fs.readFileSync(path.resolve(__dirname, 'output.js.map'), 'utf-8') + code: readFileSync(path.resolve(__dirname, 'output.js'), 'utf-8'), + map: readFileSync(path.resolve(__dirname, 'output.js.map'), 'utf-8') }; } } diff --git a/test/watch/index.js b/test/watch/index.js index f7d9124fb52..4390f3b65ef 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const { readFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -1083,9 +1084,7 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - const generated = sander.readFileSync('test/_tmp/output/bundle.js', { - encoding: 'utf-8' - }); + const generated = readFileSync('test/_tmp/output/bundle.js', 'utf8'); assert.ok(/jQuery/.test(generated)); } ]); @@ -1505,7 +1504,7 @@ describe('rollup.watch', () => { plugins: { load() { this.addWatchFile(WATCHED_ID); - return `export default "${sander.readFileSync(WATCHED_ID).toString().trim()}"`; + return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; } } }); @@ -1562,10 +1561,7 @@ describe('rollup.watch', () => { if (addWatchFile) { this.addWatchFile(WATCHED_ID); } - return `export const value = "${sander - .readFileSync(WATCHED_ID) - .toString() - .trim()}"`; + return `export const value = "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; } } } @@ -1613,7 +1609,7 @@ describe('rollup.watch', () => { transform(code, id) { if (id.endsWith('dep1.js')) { this.addWatchFile(path.resolve('test/_tmp/input/dep2.js')); - const text = sander.readFileSync('test/_tmp/input/dep2.js').toString().trim(); + const text = readFileSync('test/_tmp/input/dep2.js', 'utf8').trim(); return `export default ${JSON.stringify(text)}`; } } @@ -1800,7 +1796,7 @@ describe('rollup.watch', () => { transform() { transformRuns++; this.addWatchFile(WATCHED_ID); - return `export default "${sander.readFileSync(WATCHED_ID).toString().trim()}"`; + return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; } } }); From e50cc59bd7a2ded388aacdc19834fb27c794ed87 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 22:43:04 -0500 Subject: [PATCH 2/9] refactor: use fs.existsSync --- test/cli/index.js | 7 ++----- test/form/index.js | 5 ++--- test/watch/index.js | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/test/cli/index.js b/test/cli/index.js index 963f3d03fb5..d175968f6ed 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -1,6 +1,6 @@ const assert = require('assert'); const { exec } = require('child_process'); -const { readFileSync } = require('fs'); +const { existsSync, readFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const { @@ -99,10 +99,7 @@ runTestSuiteWithSamples( } catch (err) { done(err); } - } else if ( - sander.existsSync('_expected') && - sander.statSync('_expected').isDirectory() - ) { + } else if (existsSync('_expected') && sander.statSync('_expected').isDirectory()) { try { assertDirectoriesAreEqual('_actual', '_expected'); done(); diff --git a/test/form/index.js b/test/form/index.js index 97c1e752308..f0d1c6e4145 100644 --- a/test/form/index.js +++ b/test/form/index.js @@ -1,14 +1,13 @@ const assert = require('assert'); -const { readFileSync } = require('fs'); +const { existsSync, readFileSync } = require('fs'); const path = require('path'); -const sander = require('sander'); const rollup = require('../../dist/rollup'); const { normaliseOutput, runTestSuiteWithSamples } = require('../utils.js'); const FORMATS = ['amd', 'cjs', 'system', 'es', 'iife', 'umd']; runTestSuiteWithSamples('form', path.resolve(__dirname, 'samples'), (dir, config) => { - const isSingleFormatTest = sander.existsSync(dir + '/_expected.js'); + const isSingleFormatTest = existsSync(dir + '/_expected.js'); const itOrDescribe = isSingleFormatTest ? it : describe; (config.skip ? itOrDescribe.skip : config.solo ? itOrDescribe.only : itOrDescribe)( path.basename(dir) + ': ' + config.description, diff --git a/test/watch/index.js b/test/watch/index.js index 4390f3b65ef..1d0a3a6d41d 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const { readFileSync } = require('fs'); +const { existsSync, readFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -572,7 +572,7 @@ describe('rollup.watch', () => { 'BUNDLE_START', 'ERROR', () => { - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', @@ -613,7 +613,7 @@ describe('rollup.watch', () => { 'BUNDLE_START', 'ERROR', () => { - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', @@ -1048,11 +1048,11 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual( - sander.existsSync(path.resolve(__dirname, '../_tmp/output/bundle1.js')), + existsSync(path.resolve(__dirname, '../_tmp/output/bundle1.js')), false ); assert.strictEqual( - sander.existsSync(path.resolve(__dirname, '../_tmp/output/bundle2.js')), + existsSync(path.resolve(__dirname, '../_tmp/output/bundle2.js')), true ); assert.deepStrictEqual(run('../_tmp/output/bundle2.js'), 43); @@ -1275,7 +1275,7 @@ describe('rollup.watch', () => { 'END', () => { watchChangeCnt = 0; - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', @@ -1283,7 +1283,7 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); assert.strictEqual(watchChangeCnt, 1); sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, @@ -1292,7 +1292,7 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); assert.strictEqual(watchChangeCnt, 2); sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, @@ -1301,7 +1301,7 @@ describe('rollup.watch', () => { 'BUNDLE_END', // 'END', evt => { - assert.strictEqual(sander.existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); assert.strictEqual(watchChangeCnt, 3); // still aware of its output destination assert.strictEqual(evt.output[0], path.resolve('test/_tmp/output/bundle.js')); @@ -1658,7 +1658,7 @@ describe('rollup.watch', () => { plugins: { transform() { this.addWatchFile('test/_tmp/input'); - return `export default ${sander.existsSync(WATCHED_ID)}`; + return `export default ${existsSync(WATCHED_ID)}`; } } }); @@ -1699,7 +1699,7 @@ describe('rollup.watch', () => { plugins: { transform() { this.addWatchFile('test/_tmp/input/dep'); - return `export default ${sander.existsSync('test/_tmp/input/dep')}`; + return `export default ${existsSync('test/_tmp/input/dep')}`; } } }); @@ -1740,7 +1740,7 @@ describe('rollup.watch', () => { plugins: { transform() { this.addWatchFile('test/_tmp/input/dep'); - return `export default ${sander.existsSync('test/_tmp/input/dep')}`; + return `export default ${existsSync('test/_tmp/input/dep')}`; } } }); From a844998ed14394782bfffe06896e84a0916d84c6 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 23:16:30 -0500 Subject: [PATCH 3/9] refactor: use fs.writeFileSync --- .../wait-for-bundle-input-object/_config.js | 10 +- .../samples/wait-for-bundle-input/_config.js | 6 +- .../cli/samples/watch/bundle-error/_config.js | 8 +- .../watch-config-early-update/_config.js | 10 +- .../watch/watch-config-error/_config.js | 10 +- .../watch-config-initial-error/_config.js | 8 +- .../watch/watch-config-no-update/_config.js | 8 +- test/watch/index.js | 108 +++++++++--------- 8 files changed, 84 insertions(+), 84 deletions(-) diff --git a/test/cli/samples/wait-for-bundle-input-object/_config.js b/test/cli/samples/wait-for-bundle-input-object/_config.js index 981f3ba47a9..f218c75e491 100644 --- a/test/cli/samples/wait-for-bundle-input-object/_config.js +++ b/test/cli/samples/wait-for-bundle-input-object/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let second; @@ -12,14 +12,14 @@ module.exports = { third = path.resolve(__dirname, 'third.js'); }, after() { - fs.unlinkSync(second); - fs.unlinkSync(third); + unlinkSync(second); + unlinkSync(third); }, abortOnStderr(data) { if (data.includes('waiting for input second')) { - fs.writeFileSync(second, "export default 'second'"); + writeFileSync(second, "export default 'second'"); } else if (data.includes('waiting for input third')) { - fs.writeFileSync(third, "export default 'third'"); + writeFileSync(third, "export default 'third'"); } } }; diff --git a/test/cli/samples/wait-for-bundle-input/_config.js b/test/cli/samples/wait-for-bundle-input/_config.js index 045097c443b..f14fcf1084d 100644 --- a/test/cli/samples/wait-for-bundle-input/_config.js +++ b/test/cli/samples/wait-for-bundle-input/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let mainFile; @@ -10,12 +10,12 @@ module.exports = { mainFile = path.resolve(__dirname, 'main.js'); }, after() { - fs.unlinkSync(mainFile); + unlinkSync(mainFile); }, abortOnStderr(data) { if (data.includes('waiting for input main.js')) { // wait longer than one polling interval - setTimeout(() => fs.writeFileSync(mainFile, 'export default 42;'), 600); + setTimeout(() => writeFileSync(mainFile, 'export default 42;'), 600); } } }; diff --git a/test/cli/samples/watch/bundle-error/_config.js b/test/cli/samples/watch/bundle-error/_config.js index 7b101a1bc14..f056fe41ba7 100644 --- a/test/cli/samples/watch/bundle-error/_config.js +++ b/test/cli/samples/watch/bundle-error/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let mainFile; @@ -8,15 +8,15 @@ module.exports = { command: 'rollup -cw', before() { mainFile = path.resolve(__dirname, 'main.js'); - fs.writeFileSync(mainFile, '<=>'); + writeFileSync(mainFile, '<=>'); }, after() { // synchronous sometimes does not seem to work, probably because the watch is not yet removed properly - setTimeout(() => fs.unlinkSync(mainFile), 300); + setTimeout(() => unlinkSync(mainFile), 300); }, abortOnStderr(data) { if (data.includes('Error: Unexpected token')) { - setTimeout(() => fs.writeFileSync(mainFile, 'export default 42;'), 500); + setTimeout(() => writeFileSync(mainFile, 'export default 42;'), 500); return false; } if (data.includes('created _actual')) { diff --git a/test/cli/samples/watch/watch-config-early-update/_config.js b/test/cli/samples/watch/watch-config-early-update/_config.js index c13c2690ab6..2f5fdb8c9aa 100644 --- a/test/cli/samples/watch/watch-config-early-update/_config.js +++ b/test/cli/samples/watch/watch-config-early-update/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { mkdirSync, unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let configFile; @@ -7,9 +7,9 @@ module.exports = { description: 'immediately reloads the config file if a change happens while it is parsed', command: 'rollup -cw', before() { - fs.mkdirSync(path.resolve(__dirname, '_actual')); + mkdirSync(path.resolve(__dirname, '_actual')); configFile = path.resolve(__dirname, 'rollup.config.js'); - fs.writeFileSync( + writeFileSync( configFile, ` console.error('initial'); @@ -30,11 +30,11 @@ module.exports = { ); }, after() { - fs.unlinkSync(configFile); + unlinkSync(configFile); }, abortOnStderr(data) { if (data === 'initial\n') { - fs.writeFileSync( + writeFileSync( configFile, ` console.error('updated'); diff --git a/test/cli/samples/watch/watch-config-error/_config.js b/test/cli/samples/watch/watch-config-error/_config.js index caa161f4052..f0d9adbadca 100644 --- a/test/cli/samples/watch/watch-config-error/_config.js +++ b/test/cli/samples/watch/watch-config-error/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let configFile; @@ -8,7 +8,7 @@ module.exports = { command: 'rollup -cw', before() { configFile = path.resolve(__dirname, 'rollup.config.js'); - fs.writeFileSync( + writeFileSync( configFile, 'export default {\n' + '\tinput: "main.js",\n' + @@ -21,16 +21,16 @@ module.exports = { }, after() { // synchronous sometimes does not seem to work, probably because the watch is not yet removed properly - setTimeout(() => fs.unlinkSync(configFile), 300); + setTimeout(() => unlinkSync(configFile), 300); }, abortOnStderr(data) { if (data.includes(`created _actual${path.sep}main1.js`)) { - fs.writeFileSync(configFile, 'throw new Error("Config contains errors");'); + writeFileSync(configFile, 'throw new Error("Config contains errors");'); return false; } if (data.includes('Config contains errors')) { setTimeout(() => { - fs.writeFileSync( + writeFileSync( configFile, 'export default {\n' + '\tinput: "main.js",\n' + diff --git a/test/cli/samples/watch/watch-config-initial-error/_config.js b/test/cli/samples/watch/watch-config-initial-error/_config.js index 9fd2eb4def9..bb88bc19132 100644 --- a/test/cli/samples/watch/watch-config-initial-error/_config.js +++ b/test/cli/samples/watch/watch-config-initial-error/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let configFile; @@ -8,15 +8,15 @@ module.exports = { command: 'rollup -cw', before() { configFile = path.join(__dirname, 'rollup.config.js'); - fs.writeFileSync(configFile, 'throw new Error("Config contains initial errors");'); + writeFileSync(configFile, 'throw new Error("Config contains initial errors");'); }, after() { - fs.unlinkSync(configFile); + unlinkSync(configFile); }, async abortOnStderr(data) { if (data.includes('Config contains initial errors')) { await new Promise(resolve => setTimeout(resolve, 100)); - fs.writeFileSync( + writeFileSync( configFile, 'export default {\n' + '\tinput: "main.js",\n' + diff --git a/test/cli/samples/watch/watch-config-no-update/_config.js b/test/cli/samples/watch/watch-config-no-update/_config.js index 635d77d42d9..66788c59e1c 100644 --- a/test/cli/samples/watch/watch-config-no-update/_config.js +++ b/test/cli/samples/watch/watch-config-no-update/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync, writeFileSync } = require('fs'); const path = require('path'); let configFile; @@ -16,14 +16,14 @@ module.exports = { command: 'rollup -cw', before() { configFile = path.resolve(__dirname, 'rollup.config.js'); - fs.writeFileSync(configFile, configContent); + writeFileSync(configFile, configContent); }, after() { - fs.unlinkSync(configFile); + unlinkSync(configFile); }, abortOnStderr(data) { if (data.includes('created _actual/main.js')) { - fs.writeFileSync(configFile, configContent); + writeFileSync(configFile, configContent); return new Promise(resolve => setTimeout(() => resolve(true), 500)); } }, diff --git a/test/watch/index.js b/test/watch/index.js index 1d0a3a6d41d..ded8493e8d0 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const { existsSync, readFileSync } = require('fs'); +const { existsSync, readFileSync, writeFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -90,7 +90,7 @@ describe('rollup.watch', () => { if (triggerRestart) { triggerRestart = false; return wait(100) - .then(() => sander.writeFileSync('test/_tmp/input/main.js', 'export default 44;')) + .then(() => writeFileSync('test/_tmp/input/main.js', 'export default 44;')) .then(() => wait(100)) .then(() => code); } @@ -111,7 +111,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); triggerRestart = true; - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -161,7 +161,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync( + writeFileSync( 'test/_tmp/input/main.js', "import {value} from 'virtual';\nexport default value + 1;" ); @@ -204,7 +204,7 @@ describe('rollup.watch', () => { () => { watchChangeCnt = 0; assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -213,7 +213,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 43); assert.strictEqual(watchChangeCnt, 1); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -222,7 +222,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 43); assert.strictEqual(watchChangeCnt, 2); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -273,7 +273,7 @@ describe('rollup.watch', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); assert.deepStrictEqual(events, []); assert.deepStrictEqual(ids, expectedIds); - sander.writeFileSync(WATCHED_ID, 'first'); + writeFileSync(WATCHED_ID, 'first'); }, 'START', 'BUNDLE_START', @@ -283,7 +283,7 @@ describe('rollup.watch', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); assert.deepStrictEqual(events, ['create']); assert.deepStrictEqual(ids, expectedIds); - sander.writeFileSync(WATCHED_ID, 'first'); + writeFileSync(WATCHED_ID, 'first'); }, 'START', 'BUNDLE_START', @@ -346,7 +346,7 @@ describe('rollup.watch', () => { 'END', async () => { assert.strictEqual(lastEvent, null); - sander.writeFileSync(WATCHED_ID, 'another'); + writeFileSync(WATCHED_ID, 'another'); await wait(100); sander.unlinkSync(WATCHED_ID); }, @@ -357,11 +357,11 @@ describe('rollup.watch', () => { async () => { assert.strictEqual(lastEvent, 'delete'); lastEvent = null; - sander.writeFileSync(WATCHED_ID, '123'); + writeFileSync(WATCHED_ID, '123'); await wait(100); sander.unlinkSync(WATCHED_ID); // To ensure there is always another change to trigger a rebuild - sander.writeFileSync(MAIN_ID, 'export default 43;'); + writeFileSync(MAIN_ID, 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -369,9 +369,9 @@ describe('rollup.watch', () => { 'END', async () => { assert.strictEqual(lastEvent, null); - sander.writeFileSync(WATCHED_ID, '123'); + writeFileSync(WATCHED_ID, '123'); await wait(100); - sander.writeFileSync(WATCHED_ID, 'asd'); + writeFileSync(WATCHED_ID, 'asd'); }, 'START', 'BUNDLE_START', @@ -460,7 +460,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/main1.js'), 21); assert.strictEqual(run('../_tmp/output/main2.js'), 42); - sander.writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); + writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); }, 'START', 'BUNDLE_START', @@ -499,7 +499,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/_main_1.js'), 21); assert.strictEqual(run('../_tmp/output/subfolder/_main_2.js'), 42); - sander.writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); + writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); }, 'START', 'BUNDLE_START', @@ -534,13 +534,13 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/main.js', 'export nope;'); + writeFileSync('test/_tmp/input/main.js', 'export nope;'); }, 'START', 'BUNDLE_START', 'ERROR', () => { - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -573,7 +573,7 @@ describe('rollup.watch', () => { 'ERROR', () => { assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -614,7 +614,7 @@ describe('rollup.watch', () => { 'ERROR', () => { assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -649,14 +649,14 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); sander.unlinkSync('test/_tmp/input/main.js'); - sander.writeFileSync('test/_tmp/input/main.js', 'export nope;'); + writeFileSync('test/_tmp/input/main.js', 'export nope;'); }, 'START', 'BUNDLE_START', 'ERROR', () => { sander.unlinkSync('test/_tmp/input/main.js'); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -691,14 +691,14 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 43); sander.unlinkSync('test/_tmp/input/dep.js'); - sander.writeFileSync('test/_tmp/input/dep.js', 'export nope;'); + writeFileSync('test/_tmp/input/dep.js', 'export nope;'); }, 'START', 'BUNDLE_START', 'ERROR', () => { sander.unlinkSync('test/_tmp/input/dep.js'); - sander.writeFileSync('test/_tmp/input/dep.js', 'export const value = 43;'); + writeFileSync('test/_tmp/input/dep.js', 'export const value = 43;'); }, 'START', 'BUNDLE_START', @@ -736,7 +736,7 @@ describe('rollup.watch', () => { 'START', 'BUNDLE_START', () => { - sander.writeFileSync('test/_tmp/input/main.js', 'export default 44;'); + writeFileSync('test/_tmp/input/main.js', 'export default 44;'); return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); } ]); @@ -768,7 +768,7 @@ describe('rollup.watch', () => { 'START', 'BUNDLE_START', () => { - sander.writeFileSync('test/_tmp/input/main.js', 'export default 44;'); + writeFileSync('test/_tmp/input/main.js', 'export default 44;'); return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); } ]); @@ -796,7 +796,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 42;'); + writeFileSync('test/_tmp/input/main.js', 'export default 42;'); }, 'START', 'BUNDLE_START', @@ -808,7 +808,7 @@ describe('rollup.watch', () => { watcher.once('event', event => { unexpectedEvent = event; }); - sander.writeFileSync('test/_tmp/input/dep.js', '= invalid'); + writeFileSync('test/_tmp/input/dep.js', '= invalid'); return wait(400).then(() => assert.strictEqual(unexpectedEvent, false)); } ]); @@ -836,14 +836,14 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/main.js', `import '../output/bundle.js'`); + writeFileSync('test/_tmp/input/main.js', `import '../output/bundle.js'`); }, 'START', 'BUNDLE_START', 'ERROR', event => { assert.strictEqual(event.error.message, 'Cannot import the generated bundle'); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -883,7 +883,7 @@ describe('rollup.watch', () => { foo: 'foo-1', bar: 'bar-1' }); - sander.writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); + writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); }, 'START', 'BUNDLE_START', @@ -898,7 +898,7 @@ describe('rollup.watch', () => { watcher.once('event', event => { unexpectedEvent = event; }); - sander.writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); + writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); return wait(400).then(() => { assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { foo: 'foo-2', @@ -938,7 +938,7 @@ describe('rollup.watch', () => { foo: 'foo-1', bar: 'bar-1' }); - sander.writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); + writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); }, 'START', 'BUNDLE_START', @@ -953,7 +953,7 @@ describe('rollup.watch', () => { watcher.once('event', event => { unexpectedEvent = event; }); - sander.writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); + writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); return wait(400).then(() => { assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { foo: 'foo-2', @@ -1001,7 +1001,7 @@ describe('rollup.watch', () => { () => { assert.deepStrictEqual(run('../_tmp/output/bundle1.js'), 42); assert.deepStrictEqual(run('../_tmp/output/bundle2.js'), 43); - sander.writeFileSync('test/_tmp/input/main2.js', 'export default 44'); + writeFileSync('test/_tmp/input/main2.js', 'export default 44'); }, 'START', 'BUNDLE_START', @@ -1112,7 +1112,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/[foo]/bar.js', `export const bar = 43;`); + writeFileSync('test/_tmp/input/[foo]/bar.js', `export const bar = 43;`); }, 'START', 'BUNDLE_START', @@ -1154,7 +1154,7 @@ describe('rollup.watch', () => { sander.rimrafSync('test/_tmp/output'); // this should only update the hash of that particular entry point - sander.writeFileSync( + writeFileSync( 'test/_tmp/input/main-static.js', "import {value} from './shared';\nexport default 2 * value;" ); @@ -1174,7 +1174,7 @@ describe('rollup.watch', () => { staticName = newStaticName; // this should update all hashes - sander.writeFileSync('test/_tmp/input/shared.js', 'export const value = 42;'); + writeFileSync('test/_tmp/input/shared.js', 'export const value = 42;'); }, 'START', 'BUNDLE_START', @@ -1276,7 +1276,7 @@ describe('rollup.watch', () => { () => { watchChangeCnt = 0; assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -1285,7 +1285,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); assert.strictEqual(watchChangeCnt, 1); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -1294,7 +1294,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); assert.strictEqual(watchChangeCnt, 2); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', 'BUNDLE_START', @@ -1331,7 +1331,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); startTime = process.hrtime(); }, 'START', @@ -1405,7 +1405,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); startTime = process.hrtime(); }, 'START', @@ -1438,7 +1438,7 @@ describe('rollup.watch', () => { .copydir('test/watch/samples/basic') .to('test/_tmp/input') .then(() => { - for (const file of watchFiles) sander.writeFileSync(file, 'initial'); + for (const file of watchFiles) writeFileSync(file, 'initial'); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', output: { @@ -1474,7 +1474,7 @@ describe('rollup.watch', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); // sometimes the watcher is triggered during the initial run watchChangeIds.clear(); - for (const file of watchFiles) sander.writeFileSync(file, 'changed'); + for (const file of watchFiles) writeFileSync(file, 'changed'); }, 'START', 'BUNDLE_START', @@ -1516,7 +1516,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); - sander.writeFileSync(WATCHED_ID, 'next'); + writeFileSync(WATCHED_ID, 'next'); }, 'START', 'BUNDLE_START', @@ -1577,7 +1577,7 @@ describe('rollup.watch', () => { () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); addWatchFile = false; - sander.writeFileSync(WATCHED_ID, 'next'); + writeFileSync(WATCHED_ID, 'next'); }, 'START', 'BUNDLE_START', @@ -1585,7 +1585,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 'next'); - sander.writeFileSync(WATCHED_ID, 'other'); + writeFileSync(WATCHED_ID, 'other'); events.length = 0; return wait(400).then(() => assert.deepStrictEqual(events, [])); } @@ -1626,7 +1626,7 @@ describe('rollup.watch', () => { run('../_tmp/output/bundle.js'), `dep1: "export default 'dep2';", dep2: "dep2"` ); - sander.writeFileSync('test/_tmp/input/dep2.js', 'export default "next";'); + writeFileSync('test/_tmp/input/dep2.js', 'export default "next";'); }, 'START', 'BUNDLE_START', @@ -1711,7 +1711,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), false); - sander.writeFileSync('test/_tmp/input/dep', ''); + writeFileSync('test/_tmp/input/dep', ''); }, 'START', 'BUNDLE_START', @@ -1729,7 +1729,7 @@ describe('rollup.watch', () => { .copydir('test/watch/samples/basic') .to('test/_tmp/input') .then(() => { - sander.writeFileSync('test/_tmp/input/dep', ''); + writeFileSync('test/_tmp/input/dep', ''); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', output: { @@ -1760,7 +1760,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), false); - sander.writeFileSync('test/_tmp/input/dep', ''); + writeFileSync('test/_tmp/input/dep', ''); }, 'START', 'BUNDLE_START', @@ -1781,7 +1781,7 @@ describe('rollup.watch', () => { .to('test/_tmp/input') .then(() => wait(100)) .then(() => { - sander.writeFileSync('test/_tmp/input/alsoWatched', 'initial'); + writeFileSync('test/_tmp/input/alsoWatched', 'initial'); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', output: { @@ -1808,7 +1808,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(transformRuns, 1); - sander.writeFileSync('test/_tmp/input/alsoWatched', 'next'); + writeFileSync('test/_tmp/input/alsoWatched', 'next'); }, 'START', 'BUNDLE_START', From 6b0a7b8e4d87e6ab676b03f69265e356f6e297a1 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 23:20:57 -0500 Subject: [PATCH 4/9] refactor: use fs.unlinkSync --- .../samples/warn-broken-sourcemap/_config.js | 6 ++--- .../samples/warn-broken-sourcemaps/_config.js | 6 ++--- test/utils.js | 3 ++- test/watch/index.js | 22 +++++++++---------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/test/cli/samples/warn-broken-sourcemap/_config.js b/test/cli/samples/warn-broken-sourcemap/_config.js index 44597f2d557..7ec82baaf01 100644 --- a/test/cli/samples/warn-broken-sourcemap/_config.js +++ b/test/cli/samples/warn-broken-sourcemap/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync } = require('fs'); const path = require('path'); const { assertIncludes } = require('../../../utils.js'); @@ -6,8 +6,8 @@ module.exports = { description: 'displays warnings for broken sourcemaps', command: 'rollup -c', stderr: stderr => { - fs.unlinkSync(path.resolve(__dirname, 'bundle')); - fs.unlinkSync(path.resolve(__dirname, 'bundle.map')); + unlinkSync(path.resolve(__dirname, 'bundle')); + unlinkSync(path.resolve(__dirname, 'bundle.map')); assertIncludes( stderr, '(!) Broken sourcemap\n' + diff --git a/test/cli/samples/warn-broken-sourcemaps/_config.js b/test/cli/samples/warn-broken-sourcemaps/_config.js index b3e2e2ba768..a6aafd97a38 100644 --- a/test/cli/samples/warn-broken-sourcemaps/_config.js +++ b/test/cli/samples/warn-broken-sourcemaps/_config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { unlinkSync } = require('fs'); const path = require('path'); const { assertIncludes } = require('../../../utils.js'); @@ -6,8 +6,8 @@ module.exports = { description: 'displays warnings for broken sourcemaps', command: 'rollup -c', stderr: stderr => { - fs.unlinkSync(path.resolve(__dirname, 'bundle')); - fs.unlinkSync(path.resolve(__dirname, 'bundle.map')); + unlinkSync(path.resolve(__dirname, 'bundle')); + unlinkSync(path.resolve(__dirname, 'bundle.map')); assertIncludes( stderr, '(!) Broken sourcemap\n' + diff --git a/test/utils.js b/test/utils.js index beb2c60d97b..ae9be43197a 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const { unlinkSync } = require('fs'); const path = require('path'); const fixturify = require('fixturify'); const sander = require('sander'); @@ -151,7 +152,7 @@ function getFileNamesAndRemoveOutput(dir) { return false; } if (fileName === '_actual.js') { - sander.unlinkSync(path.join(dir, '_actual.js')); + unlinkSync(path.join(dir, '_actual.js')); return false; } return true; diff --git a/test/watch/index.js b/test/watch/index.js index ded8493e8d0..c305e13df9e 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const { existsSync, readFileSync, writeFileSync } = require('fs'); +const { existsSync, promises, readFileSync, unlinkSync, writeFileSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -241,7 +241,7 @@ describe('rollup.watch', () => { let ids; const expectedIds = [WATCHED_ID, path.resolve('test/_tmp/input/main.js')]; await sander.copydir('test/watch/samples/watch-files').to('test/_tmp/input'); - await sander.unlink(WATCHED_ID); + await promises.unlink(WATCHED_ID); await wait(100); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', @@ -293,7 +293,7 @@ describe('rollup.watch', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); assert.deepStrictEqual(events, ['create', 'update']); assert.deepStrictEqual(ids, expectedIds); - sander.unlinkSync(WATCHED_ID); + unlinkSync(WATCHED_ID); }, 'START', 'BUNDLE_START', @@ -348,7 +348,7 @@ describe('rollup.watch', () => { assert.strictEqual(lastEvent, null); writeFileSync(WATCHED_ID, 'another'); await wait(100); - sander.unlinkSync(WATCHED_ID); + unlinkSync(WATCHED_ID); }, 'START', 'BUNDLE_START', @@ -359,7 +359,7 @@ describe('rollup.watch', () => { lastEvent = null; writeFileSync(WATCHED_ID, '123'); await wait(100); - sander.unlinkSync(WATCHED_ID); + unlinkSync(WATCHED_ID); // To ensure there is always another change to trigger a rebuild writeFileSync(MAIN_ID, 'export default 43;'); }, @@ -648,14 +648,14 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - sander.unlinkSync('test/_tmp/input/main.js'); + unlinkSync('test/_tmp/input/main.js'); writeFileSync('test/_tmp/input/main.js', 'export nope;'); }, 'START', 'BUNDLE_START', 'ERROR', () => { - sander.unlinkSync('test/_tmp/input/main.js'); + unlinkSync('test/_tmp/input/main.js'); writeFileSync('test/_tmp/input/main.js', 'export default 43;'); }, 'START', @@ -690,14 +690,14 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - sander.unlinkSync('test/_tmp/input/dep.js'); + unlinkSync('test/_tmp/input/dep.js'); writeFileSync('test/_tmp/input/dep.js', 'export nope;'); }, 'START', 'BUNDLE_START', 'ERROR', () => { - sander.unlinkSync('test/_tmp/input/dep.js'); + unlinkSync('test/_tmp/input/dep.js'); writeFileSync('test/_tmp/input/dep.js', 'export const value = 43;'); }, 'START', @@ -1670,7 +1670,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), true); - sander.unlinkSync(WATCHED_ID); + unlinkSync(WATCHED_ID); }, 'START', 'BUNDLE_START', @@ -1752,7 +1752,7 @@ describe('rollup.watch', () => { 'END', () => { assert.strictEqual(run('../_tmp/output/bundle.js'), true); - sander.unlinkSync('test/_tmp/input/dep'); + unlinkSync('test/_tmp/input/dep'); }, 'START', 'BUNDLE_START', From 82e5329cb9153ebe0d8ba31f5243e3b3d35ac04e Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 23:29:32 -0500 Subject: [PATCH 5/9] refactor: use fs.readdirSync --- scripts/load-perf-config.js | 4 ++-- scripts/perf-init.js | 4 ++-- test/hooks/index.js | 3 ++- test/utils.js | 8 ++++---- test/watch/index.js | 11 +++++++++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/scripts/load-perf-config.js b/scripts/load-perf-config.js index 24cb396e93b..647ee9b720f 100644 --- a/scripts/load-perf-config.js +++ b/scripts/load-perf-config.js @@ -1,4 +1,4 @@ -const fs = require('fs'); +const { accessSync, constants } = require('fs'); const path = require('path'); const rollup = require('../dist/rollup.js'); @@ -6,7 +6,7 @@ exports.targetDir = path.resolve(__dirname, '..', 'perf'); const configFile = path.resolve(exports.targetDir, 'rollup.config.js'); try { - fs.accessSync(configFile, fs.constants.R_OK); + accessSync(configFile, constants.R_OK); } catch (e) { console.error( `No valid "rollup.config.js" in ${exports.targetDir}. Did you "npm run perf:init"?` diff --git a/scripts/perf-init.js b/scripts/perf-init.js index 6cd860f73c1..2411e3a8a4e 100644 --- a/scripts/perf-init.js +++ b/scripts/perf-init.js @@ -1,6 +1,6 @@ /* eslint-disable no-console */ -const fs = require('fs'); +const { accessSync, constants } = require('fs'); const path = require('path'); const execa = require('execa'); const sander = require('sander'); @@ -43,7 +43,7 @@ async function setupNewRepo(repo, branch) { gitArgs.push(`https://github.com/${repo}.git`, TARGET_DIR); await execWithOutput('git', gitArgs); try { - fs.accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), fs.constants.R_OK); + accessSync(path.resolve(TARGET_DIR, 'rollup.config.js'), constants.R_OK); } catch (e) { throw new Error('The repository needs to have a file "rollup.config.js" at the top level.'); } diff --git a/test/hooks/index.js b/test/hooks/index.js index d0f04609ae1..7470e8fe464 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const { readdirSync } = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup.js'); @@ -33,7 +34,7 @@ describe('hooks', () => { }) ) .then(() => { - const fileNames = sander.readdirSync(TEMP_DIR).sort(); + const fileNames = readdirSync(TEMP_DIR).sort(); assert.deepStrictEqual(fileNames, ['chunk.js', 'input.js']); return sander.rimraf(TEMP_DIR); })); diff --git a/test/utils.js b/test/utils.js index ae9be43197a..1c7f5e6ff33 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const { unlinkSync } = require('fs'); +const { readdirSync, unlinkSync } = require('fs'); const path = require('path'); const fixturify = require('fixturify'); const sander = require('sander'); @@ -120,8 +120,8 @@ function runSamples(samplesDir, runTest, onTeardown) { if (onTeardown) { afterEach(onTeardown); } - sander - .readdirSync(samplesDir) + + readdirSync(samplesDir) .filter(name => name[0] !== '.') .sort() .forEach(fileName => runTestsInDir(path.join(samplesDir, fileName), runTest)); @@ -146,7 +146,7 @@ function runTestsInDir(dir, runTest) { function getFileNamesAndRemoveOutput(dir) { try { - return sander.readdirSync(dir).filter(fileName => { + return readdirSync(dir).filter(fileName => { if (fileName === '_actual') { sander.rimrafSync(path.join(dir, '_actual')); return false; diff --git a/test/watch/index.js b/test/watch/index.js index c305e13df9e..5fd2e0a8dc2 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1,5 +1,12 @@ const assert = require('assert'); -const { existsSync, promises, readFileSync, unlinkSync, writeFileSync } = require('fs'); +const { + existsSync, + promises, + readdirSync, + readFileSync, + unlinkSync, + writeFileSync +} = require('fs'); const path = require('path'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -1150,7 +1157,7 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - [dynamicName, staticName, chunkName] = sander.readdirSync('test/_tmp/output').sort(); + [dynamicName, staticName, chunkName] = readdirSync('test/_tmp/output').sort(); sander.rimrafSync('test/_tmp/output'); // this should only update the hash of that particular entry point From 8461d207c230fc58892edc45c5a367aeb5444dcc Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 25 Dec 2021 23:51:24 -0500 Subject: [PATCH 6/9] refactor: use fs-extra.removeSync --- package-lock.json | 53 ++++++++++++++++++++++++++++++++++---------- package.json | 1 + scripts/perf-init.js | 4 ++-- test/hooks/index.js | 6 ++--- test/utils.js | 6 ++--- test/watch/index.js | 17 +++++++------- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1c33476fea8..d0e97dca0ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1991,6 +1991,34 @@ "fs-extra": "^8.1.0", "matcher-collection": "^2.0.1", "walk-sync": "^2.0.2" + }, + "dependencies": { + "fs-extra": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", + "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "dev": true, + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^4.0.0", + "universalify": "^0.1.0" + } + }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "dev": true, + "requires": { + "graceful-fs": "^4.1.6" + } + }, + "universalify": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "dev": true + } } }, "flat": { @@ -2032,14 +2060,14 @@ "dev": true }, "fs-extra": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz", - "integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==", + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", + "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", "dev": true, "requires": { "graceful-fs": "^4.2.0", - "jsonfile": "^4.0.0", - "universalify": "^0.1.0" + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" } }, "fs.realpath": { @@ -2740,12 +2768,13 @@ } }, "jsonfile": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", - "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", "dev": true, "requires": { - "graceful-fs": "^4.1.6" + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" } }, "levn": { @@ -4634,9 +4663,9 @@ "dev": true }, "universalify": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", "dev": true }, "uri-js": { diff --git a/package.json b/package.json index 857e8ee89c2..240d4a6cdf5 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "eslint-plugin-prettier": "^4.0.0", "execa": "^5.1.1", "fixturify": "^2.1.1", + "fs-extra": "^10.0.0", "hash.js": "^1.1.7", "husky": "^7.0.4", "is-reference": "^3.0.0", diff --git a/scripts/perf-init.js b/scripts/perf-init.js index 2411e3a8a4e..72e617d7db6 100644 --- a/scripts/perf-init.js +++ b/scripts/perf-init.js @@ -3,7 +3,7 @@ const { accessSync, constants } = require('fs'); const path = require('path'); const execa = require('execa'); -const sander = require('sander'); +const { removeSync } = require('fs-extra'); const repoWithBranch = process.argv[2]; const TARGET_DIR = path.resolve(__dirname, '..', 'perf'); @@ -17,7 +17,7 @@ if (process.argv.length !== 3 || !VALID_REPO.test(repoWithBranch)) { process.exit(1); } console.error(`Cleaning up '${TARGET_DIR}'...`); -sander.rimrafSync(TARGET_DIR); +removeSync(TARGET_DIR); const [, repo, , branch] = VALID_REPO.exec(repoWithBranch); diff --git a/test/hooks/index.js b/test/hooks/index.js index 7470e8fe464..b776bb9e808 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -1,7 +1,7 @@ const assert = require('assert'); const { readdirSync } = require('fs'); const path = require('path'); -const sander = require('sander'); +const { removeSync } = require('fs-extra'); const rollup = require('../../dist/rollup.js'); const { loader } = require('../utils.js'); @@ -36,7 +36,7 @@ describe('hooks', () => { .then(() => { const fileNames = readdirSync(TEMP_DIR).sort(); assert.deepStrictEqual(fileNames, ['chunk.js', 'input.js']); - return sander.rimraf(TEMP_DIR); + return removeSync(TEMP_DIR); })); it('supports buildStart and buildEnd hooks', () => { @@ -575,7 +575,7 @@ describe('hooks', () => { .then(bundle => bundle.write({ format: 'es', file })) .then(() => { assert.strictEqual(callCount, 1); - return sander.rimraf(TEMP_DIR); + return removeSync(TEMP_DIR); }); }); diff --git a/test/utils.js b/test/utils.js index 1c7f5e6ff33..b9bb8180b4d 100644 --- a/test/utils.js +++ b/test/utils.js @@ -2,7 +2,7 @@ const assert = require('assert'); const { readdirSync, unlinkSync } = require('fs'); const path = require('path'); const fixturify = require('fixturify'); -const sander = require('sander'); +const { removeSync } = require('fs-extra'); exports.compareError = compareError; exports.compareWarnings = compareWarnings; @@ -133,7 +133,7 @@ function runTestsInDir(dir, runTest) { loadConfigAndRunTest(dir, runTest); } else if (fileNames.length === 0) { console.warn(`Removing empty test directory ${dir}`); - sander.rmdirSync(dir); + removeSync(dir); } else { describe(path.basename(dir), () => { fileNames @@ -148,7 +148,7 @@ function getFileNamesAndRemoveOutput(dir) { try { return readdirSync(dir).filter(fileName => { if (fileName === '_actual') { - sander.rimrafSync(path.join(dir, '_actual')); + removeSync(path.join(dir, '_actual')); return false; } if (fileName === '_actual.js') { diff --git a/test/watch/index.js b/test/watch/index.js index 5fd2e0a8dc2..2ee8a207520 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -8,6 +8,7 @@ const { writeFileSync } = require('fs'); const path = require('path'); +const { removeSync } = require('fs-extra'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -24,7 +25,7 @@ describe('rollup.watch', () => { beforeEach(() => { process.chdir(cwd); - return sander.rimraf('test/_tmp'); + return removeSync('test/_tmp'); }); afterEach(() => { @@ -1158,7 +1159,7 @@ describe('rollup.watch', () => { 'END', () => { [dynamicName, staticName, chunkName] = readdirSync('test/_tmp/output').sort(); - sander.rimrafSync('test/_tmp/output'); + removeSync('test/_tmp/output'); // this should only update the hash of that particular entry point writeFileSync( @@ -1171,10 +1172,9 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - const [newDynamicName, newStaticName, newChunkName] = sander - .readdirSync('test/_tmp/output') - .sort(); - sander.rimrafSync('test/_tmp/output'); + const [newDynamicName, newStaticName, newChunkName] = + readdirSync('test/_tmp/output').sort(); + removeSync('test/_tmp/output'); assert.notEqual(newStaticName, staticName); assert.strictEqual(newDynamicName, dynamicName); assert.strictEqual(newChunkName, chunkName); @@ -1188,9 +1188,8 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - const [newDynamicName, newStaticName, newChunkName] = sander - .readdirSync('test/_tmp/output') - .sort(); + const [newDynamicName, newStaticName, newChunkName] = + readdirSync('test/_tmp/output').sort(); assert.notEqual(newStaticName, staticName); assert.notEqual(newDynamicName, dynamicName); assert.notEqual(newChunkName, chunkName); From 1896ce4192cb5c6f8369f612ab110e2551a1c472 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 26 Dec 2021 10:21:27 -0500 Subject: [PATCH 7/9] refactor: use fs-extra.copy/copySync --- scripts/update-snapshots.js | 13 +- test/cli/index.js | 17 +- test/watch/index.js | 2115 +++++++++++++++++------------------ 3 files changed, 1027 insertions(+), 1118 deletions(-) diff --git a/scripts/update-snapshots.js b/scripts/update-snapshots.js index b7e9ee3223a..79b6e28fde8 100755 --- a/scripts/update-snapshots.js +++ b/scripts/update-snapshots.js @@ -1,7 +1,8 @@ #!/usr/bin/env node +const { readdirSync } = require('fs'); const { resolve, join } = require('path'); -const { readdirSync, copydirSync, copyFileSync, rimrafSync } = require('sander'); +const { copySync, removeSync } = require('fs-extra'); const basePath = resolve(__dirname, '../test'); @@ -14,10 +15,10 @@ for (const dir of formDirsToHandle) { formDirsToHandle.push(...testFiles.map(filename => join(dir, filename))); } else if (testFiles.includes('_actual')) { const expectedPath = join(testPath, '_expected'); - rimrafSync(expectedPath); - copydirSync(join(testPath, '_actual')).to(expectedPath); + removeSync(expectedPath); + copySync(join(testPath, '_actual'), expectedPath); } else if (testFiles.includes('_actual.js')) { - copyFileSync(join(testPath, '_actual.js')).to(join(testPath, '_expected.js')); + copySync(join(testPath, '_actual.js'), join(testPath, '_expected.js')); } else { throw new Error(`Could not find test output in ${testPath}`); } @@ -32,8 +33,8 @@ for (const dir of chunkingDirsToHandle) { chunkingDirsToHandle.push(...testFiles.map(filename => join(dir, filename))); } else if (testFiles.includes('_actual')) { const expectedPath = join(testPath, '_expected'); - rimrafSync(expectedPath); - copydirSync(join(testPath, '_actual')).to(expectedPath); + removeSync(expectedPath); + copySync(join(testPath, '_actual'), expectedPath); } else { throw new Error(`Could not find test output in ${testPath}`); } diff --git a/test/cli/index.js b/test/cli/index.js index d175968f6ed..ae86845fde6 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -1,8 +1,9 @@ const assert = require('assert'); const { exec } = require('child_process'); const { existsSync, readFileSync } = require('fs'); -const path = require('path'); -const sander = require('sander'); +const { basename, resolve, sep } = require('path'); +const process = require('process'); +const { copySync, removeSync, statSync } = require('fs-extra'); const { normaliseOutput, runTestSuiteWithSamples, @@ -11,22 +12,22 @@ const { const cwd = process.cwd(); -sander.rimrafSync(__dirname, 'node_modules'); -sander.copydirSync(__dirname, 'node_modules_rename_me').to(__dirname, 'node_modules'); +removeSync(resolve(__dirname, 'node_modules')); +copySync(resolve(__dirname, 'node_modules_rename_me'), resolve(__dirname, 'node_modules')); runTestSuiteWithSamples( 'cli', - path.resolve(__dirname, 'samples'), + resolve(__dirname, 'samples'), (dir, config) => { (config.skip ? it.skip : config.solo ? it.only : it)( - path.basename(dir) + ': ' + config.description, + basename(dir) + ': ' + config.description, done => { process.chdir(config.cwd || dir); if (config.before) config.before(); const command = config.command.replace( /(^| )rollup($| )/g, - `node ${path.resolve(__dirname, '../../dist/bin')}${path.sep}rollup ` + `node ${resolve(__dirname, '../../dist/bin')}${sep}rollup ` ); const childProcess = exec( @@ -99,7 +100,7 @@ runTestSuiteWithSamples( } catch (err) { done(err); } - } else if (existsSync('_expected') && sander.statSync('_expected').isDirectory()) { + } else if (existsSync('_expected') && statSync('_expected').isDirectory()) { try { assertDirectoriesAreEqual('_actual', '_expected'); done(); diff --git a/test/watch/index.js b/test/watch/index.js index 2ee8a207520..bf873cd4dab 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -7,8 +7,8 @@ const { unlinkSync, writeFileSync } = require('fs'); -const path = require('path'); -const { removeSync } = require('fs-extra'); +const { resolve } = require('path'); +const { copy, removeSync } = require('fs-extra'); const sander = require('sander'); const rollup = require('../../dist/rollup'); @@ -83,111 +83,105 @@ describe('rollup.watch', () => { it('watches a file and triggers reruns if necessary', () => { let triggerRestart = false; - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - plugins: { - options(options) { - assert.strictEqual(this.meta.watchMode, true, 'watchMode in options'); - }, - transform(code) { - assert.strictEqual(this.meta.watchMode, true, 'watchMode in transform'); - if (triggerRestart) { - triggerRestart = false; - return wait(100) - .then(() => writeFileSync('test/_tmp/input/main.js', 'export default 44;')) - .then(() => wait(100)) - .then(() => code); - } - } - }, - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - triggerRestart = true; - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + plugins: { + options(options) { + assert.strictEqual(this.meta.watchMode, true, 'watchMode in options'); }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 44); + transform(code) { + assert.strictEqual(this.meta.watchMode, true, 'watchMode in transform'); + if (triggerRestart) { + triggerRestart = false; + return wait(100) + .then(() => writeFileSync('test/_tmp/input/main.js', 'export default 44;')) + .then(() => wait(100)) + .then(() => code); + } } - ]); + }, + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + triggerRestart = true; + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 44); + } + ]); + }); }); it('does not fail for virtual files', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - plugins: { - resolveId(id) { - if (id === 'virtual') { - return id; - } - }, - load(id) { - if (id === 'virtual') { - return `export const value = 42;`; - } + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + plugins: { + resolveId(id) { + if (id === 'virtual') { + return id; } }, - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - writeFileSync( - 'test/_tmp/input/main.js', - "import {value} from 'virtual';\nexport default value + 1;" - ); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + load(id) { + if (id === 'virtual') { + return `export const value = 42;`; + } } - ]); + }, + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + writeFileSync( + 'test/_tmp/input/main.js', + "import {value} from 'virtual';\nexport default value + 1;" + ); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('passes file events to the watchChange plugin hook once for each change', async () => { let watchChangeCnt = 0; - await sander.copydir('test/watch/samples/basic').to('test/_tmp/input'); + await copy('test/watch/samples/basic', 'test/_tmp/input'); await wait(100); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', @@ -199,7 +193,7 @@ describe('rollup.watch', () => { plugins: { watchChange(id) { watchChangeCnt++; - assert.strictEqual(id, path.resolve('test/_tmp/input/main.js')); + assert.strictEqual(id, resolve('test/_tmp/input/main.js')); } } }); @@ -244,11 +238,11 @@ describe('rollup.watch', () => { }); it('passes change parameter to the watchChange plugin hook', async () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); + const WATCHED_ID = resolve('test/_tmp/input/watched'); const events = []; let ids; - const expectedIds = [WATCHED_ID, path.resolve('test/_tmp/input/main.js')]; - await sander.copydir('test/watch/samples/watch-files').to('test/_tmp/input'); + const expectedIds = [WATCHED_ID, resolve('test/_tmp/input/main.js')]; + await copy('test/watch/samples/watch-files', 'test/_tmp/input'); await promises.unlink(WATCHED_ID); await wait(100); watcher = rollup.watch({ @@ -316,10 +310,10 @@ describe('rollup.watch', () => { }); it('correctly rewrites change event during build delay', async () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); - const MAIN_ID = path.resolve('test/_tmp/input/main.js'); + const WATCHED_ID = resolve('test/_tmp/input/watched'); + const MAIN_ID = resolve('test/_tmp/input/main.js'); let lastEvent = null; - await sander.copydir('test/watch/samples/watch-files').to('test/_tmp/input'); + await copy('test/watch/samples/watch-files', 'test/_tmp/input'); await wait(100); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', @@ -395,589 +389,545 @@ describe('rollup.watch', () => { let calls = 0; let ctx1; let ctx2; - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: [ + { + buildStart() { + ctx1 = this; + }, + closeWatcher() { + assert.strictEqual(ctx1, this); + calls++; + } }, - plugins: [ - { - buildStart() { - ctx1 = this; - }, - closeWatcher() { - assert.strictEqual(ctx1, this); - calls++; - } + { + buildStart() { + ctx2 = this; }, - { - buildStart() { - ctx2 = this; - }, - closeWatcher() { - assert.strictEqual(ctx2, this); - calls++; - } + closeWatcher() { + assert.strictEqual(ctx2, this); + calls++; } - ] - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - assert.ok(ctx1); - assert.ok(ctx2); - watcher.once('close', () => { - assert.strictEqual(calls, 2); - }); - watcher.close(); } - ]); + ] }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + assert.ok(ctx1); + assert.ok(ctx2); + watcher.once('close', () => { + assert.strictEqual(calls, 2); + }); + watcher.close(); + } + ]); + }); }); it('watches a file in code-splitting mode', () => { - return sander - .copydir('test/watch/samples/code-splitting') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: ['test/_tmp/input/main1.js', 'test/_tmp/input/main2.js'], - output: { - dir: 'test/_tmp/output', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/main1.js'), 21); - assert.strictEqual(run('../_tmp/output/main2.js'), 42); - writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/main1.js'), 22); - assert.strictEqual(run('../_tmp/output/main2.js'), 44); - } - ]); + return copy('test/watch/samples/code-splitting', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: ['test/_tmp/input/main1.js', 'test/_tmp/input/main2.js'], + output: { + dir: 'test/_tmp/output', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/main1.js'), 21); + assert.strictEqual(run('../_tmp/output/main2.js'), 42); + writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/main1.js'), 22); + assert.strictEqual(run('../_tmp/output/main2.js'), 44); + } + ]); + }); }); it('watches a file in code-splitting mode with an input object', () => { - return sander - .copydir('test/watch/samples/code-splitting') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: { - _main_1: 'test/_tmp/input/main1.js', - 'subfolder/_main_2': 'test/_tmp/input/main2.js' - }, - output: { - dir: 'test/_tmp/output', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/_main_1.js'), 21); - assert.strictEqual(run('../_tmp/output/subfolder/_main_2.js'), 42); - writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/_main_1.js'), 22); - assert.strictEqual(run('../_tmp/output/subfolder/_main_2.js'), 44); - } - ]); + return copy('test/watch/samples/code-splitting', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: { + _main_1: 'test/_tmp/input/main1.js', + 'subfolder/_main_2': 'test/_tmp/input/main2.js' + }, + output: { + dir: 'test/_tmp/output', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/_main_1.js'), 21); + assert.strictEqual(run('../_tmp/output/subfolder/_main_2.js'), 42); + writeFileSync('test/_tmp/input/shared.js', 'export const value = 22;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/_main_1.js'), 22); + assert.strictEqual(run('../_tmp/output/subfolder/_main_2.js'), 44); + } + ]); + }); }); it('recovers from an error', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - writeFileSync('test/_tmp/input/main.js', 'export nope;'); - }, - 'START', - 'BUNDLE_START', - 'ERROR', - () => { - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - } - ]); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); - }); - it('recovers from an error on initial build', () => { - return sander - .copydir('test/watch/samples/error') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + writeFileSync('test/_tmp/input/main.js', 'export nope;'); + }, + 'START', + 'BUNDLE_START', + 'ERROR', + () => { + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); + }); - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'ERROR', - () => { - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - } - ]); + it('recovers from an error on initial build', () => { + return copy('test/watch/samples/error', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'ERROR', + () => { + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('recovers from a plugin error on initial build', () => { let count = 0; - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - plugins: { - transform() { - if (count++ === 0) { - this.error('The first run failed, try again.'); - } + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + plugins: { + transform() { + if (count++ === 0) { + this.error('The first run failed, try again.'); } - }, - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'ERROR', - () => { - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); } - ]); + }, + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'ERROR', + () => { + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('recovers from an error even when erroring entry was "renamed" (#38)', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - unlinkSync('test/_tmp/input/main.js'); - writeFileSync('test/_tmp/input/main.js', 'export nope;'); - }, - 'START', - 'BUNDLE_START', - 'ERROR', - () => { - unlinkSync('test/_tmp/input/main.js'); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - } - ]); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + unlinkSync('test/_tmp/input/main.js'); + writeFileSync('test/_tmp/input/main.js', 'export nope;'); + }, + 'START', + 'BUNDLE_START', + 'ERROR', + () => { + unlinkSync('test/_tmp/input/main.js'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('recovers from an error even when erroring dependency was "renamed" (#38)', () => { - return sander - .copydir('test/watch/samples/dependency') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - unlinkSync('test/_tmp/input/dep.js'); - writeFileSync('test/_tmp/input/dep.js', 'export nope;'); - }, - 'START', - 'BUNDLE_START', - 'ERROR', - () => { - unlinkSync('test/_tmp/input/dep.js'); - writeFileSync('test/_tmp/input/dep.js', 'export const value = 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 44); - } - ]); + return copy('test/watch/samples/dependency', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + unlinkSync('test/_tmp/input/dep.js'); + writeFileSync('test/_tmp/input/dep.js', 'export nope;'); + }, + 'START', + 'BUNDLE_START', + 'ERROR', + () => { + unlinkSync('test/_tmp/input/dep.js'); + writeFileSync('test/_tmp/input/dep.js', 'export const value = 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 44); + } + ]); + }); }); it('handles closing the watcher during a build', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - plugins: { - load() { - watcher.close(); - } - }, - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - const events = []; - watcher.on('event', event => events.push(event.code)); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - () => { - writeFileSync('test/_tmp/input/main.js', 'export default 44;'); - return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + plugins: { + load() { + watcher.close(); } - ]); + }, + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + const events = []; + watcher.on('event', event => events.push(event.code)); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + () => { + writeFileSync('test/_tmp/input/main.js', 'export default 44;'); + return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); + } + ]); + }); }); it('handles closing the watcher during a build even if an error occurred', () => { - return sander - .copydir('test/watch/samples/error') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - plugins: { - load() { - watcher.close(); - } - }, - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - const events = []; - watcher.on('event', event => events.push(event.code)); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - () => { - writeFileSync('test/_tmp/input/main.js', 'export default 44;'); - return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); + return copy('test/watch/samples/error', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + plugins: { + load() { + watcher.close(); } - ]); + }, + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); - }); + const events = []; + watcher.on('event', event => events.push(event.code)); - it('stops watching files that are no longer part of the graph', () => { - return sander - .copydir('test/watch/samples/dependency') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + () => { + writeFileSync('test/_tmp/input/main.js', 'export default 44;'); + return wait(400).then(() => assert.deepStrictEqual(events, ['START', 'BUNDLE_START'])); + } + ]); + }); + }); - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - writeFileSync('test/_tmp/input/main.js', 'export default 42;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - let unexpectedEvent = false; - watcher.once('event', event => { - unexpectedEvent = event; - }); - writeFileSync('test/_tmp/input/dep.js', '= invalid'); - return wait(400).then(() => assert.strictEqual(unexpectedEvent, false)); - } - ]); + it('stops watching files that are no longer part of the graph', () => { + return copy('test/watch/samples/dependency', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + writeFileSync('test/_tmp/input/main.js', 'export default 42;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + let unexpectedEvent = false; + watcher.once('event', event => { + unexpectedEvent = event; + }); + writeFileSync('test/_tmp/input/dep.js', '= invalid'); + return wait(400).then(() => assert.strictEqual(unexpectedEvent, false)); + } + ]); + }); }); it('refuses to watch the output file (#15)', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - writeFileSync('test/_tmp/input/main.js', `import '../output/bundle.js'`); - }, - 'START', - 'BUNDLE_START', - 'ERROR', - event => { - assert.strictEqual(event.error.message, 'Cannot import the generated bundle'); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - } - ]); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + writeFileSync('test/_tmp/input/main.js', `import '../output/bundle.js'`); + }, + 'START', + 'BUNDLE_START', + 'ERROR', + event => { + assert.strictEqual(event.error.message, 'Cannot import the generated bundle'); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('ignores files that are not specified in options.watch.include, if given', () => { - return sander - .copydir('test/watch/samples/ignored') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - watch: { - include: ['test/_tmp/input/+(main|foo).js'] - } - }); + return copy('test/watch/samples/ignored', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + watch: { + include: ['test/_tmp/input/+(main|foo).js'] + } + }); - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { - foo: 'foo-1', - bar: 'bar-1' - }); - writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { + foo: 'foo-1', + bar: 'bar-1' + }); + writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { + foo: 'foo-2', + bar: 'bar-1' + }); + let unexpectedEvent = false; + watcher.once('event', event => { + unexpectedEvent = event; + }); + writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); + return wait(400).then(() => { assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { foo: 'foo-2', bar: 'bar-1' }); - let unexpectedEvent = false; - watcher.once('event', event => { - unexpectedEvent = event; - }); - writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); - return wait(400).then(() => { - assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { - foo: 'foo-2', - bar: 'bar-1' - }); - assert.strictEqual(unexpectedEvent, false); - }); - } - ]); - }); + assert.strictEqual(unexpectedEvent, false); + }); + } + ]); + }); }); it('ignores files that are specified in options.watch.exclude, if given', () => { - return sander - .copydir('test/watch/samples/ignored') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - watch: { - exclude: ['test/_tmp/input/bar.js'] - } - }); + return copy('test/watch/samples/ignored', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + watch: { + exclude: ['test/_tmp/input/bar.js'] + } + }); - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { - foo: 'foo-1', - bar: 'bar-1' - }); - writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { + foo: 'foo-1', + bar: 'bar-1' + }); + writeFileSync('test/_tmp/input/foo.js', `export default 'foo-2';`); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { + foo: 'foo-2', + bar: 'bar-1' + }); + let unexpectedEvent = false; + watcher.once('event', event => { + unexpectedEvent = event; + }); + writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); + return wait(400).then(() => { assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { foo: 'foo-2', bar: 'bar-1' }); - let unexpectedEvent = false; - watcher.once('event', event => { - unexpectedEvent = event; - }); - writeFileSync('test/_tmp/input/bar.js', "export default 'bar-2';"); - return wait(400).then(() => { - assert.deepStrictEqual(run('../_tmp/output/bundle.js'), { - foo: 'foo-2', - bar: 'bar-1' - }); - assert.strictEqual(unexpectedEvent, false); - }); - } - ]); - }); + assert.strictEqual(unexpectedEvent, false); + }); + } + ]); + }); }); it('only rebuilds the appropriate configs', () => { - return sander - .copydir('test/watch/samples/multiple') - .to('test/_tmp/input') + return copy('test/watch/samples/multiple', 'test/_tmp/input') .then(() => wait(100)) .then(() => { watcher = rollup.watch([ @@ -1024,9 +974,7 @@ describe('rollup.watch', () => { }); it('allows watching only some configs', () => { - return sander - .copydir('test/watch/samples/multiple') - .to('test/_tmp/input') + return copy('test/watch/samples/multiple', 'test/_tmp/input') .then(() => wait(100)) .then(() => { watcher = rollup.watch([ @@ -1055,14 +1003,8 @@ describe('rollup.watch', () => { 'BUNDLE_END', 'END', () => { - assert.strictEqual( - existsSync(path.resolve(__dirname, '../_tmp/output/bundle1.js')), - false - ); - assert.strictEqual( - existsSync(path.resolve(__dirname, '../_tmp/output/bundle2.js')), - true - ); + assert.strictEqual(existsSync(resolve(__dirname, '../_tmp/output/bundle1.js')), false); + assert.strictEqual(existsSync(resolve(__dirname, '../_tmp/output/bundle2.js')), true); assert.deepStrictEqual(run('../_tmp/output/bundle2.js'), 43); } ]); @@ -1070,132 +1012,123 @@ describe('rollup.watch', () => { }); it('respects output.globals', () => { - return sander - .copydir('test/watch/samples/globals') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'iife', - globals: { - jquery: 'jQuery' - } - }, - external: ['jquery'] - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - const generated = readFileSync('test/_tmp/output/bundle.js', 'utf8'); - assert.ok(/jQuery/.test(generated)); + return copy('test/watch/samples/globals', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'iife', + globals: { + jquery: 'jQuery' } - ]); + }, + external: ['jquery'] }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + const generated = readFileSync('test/_tmp/output/bundle.js', 'utf8'); + assert.ok(/jQuery/.test(generated)); + } + ]); + }); }); it('treats filenames literally, not as globs', () => { - return sander - .copydir('test/watch/samples/non-glob') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - writeFileSync('test/_tmp/input/[foo]/bar.js', `export const bar = 43;`); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 43); - } - ]); + return copy('test/watch/samples/non-glob', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + writeFileSync('test/_tmp/input/[foo]/bar.js', `export const bar = 43;`); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 43); + } + ]); + }); }); it('updates the right hashes on dependency changes', () => { let dynamicName; let staticName; let chunkName; - return sander - .copydir('test/watch/samples/hashing') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: ['test/_tmp/input/main-static.js', 'test/_tmp/input/main-dynamic.js'], - output: { - dir: 'test/_tmp/output', - format: 'cjs', - exports: 'auto', - entryFileNames: '[name].[hash].js', - chunkFileNames: '[name].[hash].js' - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - [dynamicName, staticName, chunkName] = readdirSync('test/_tmp/output').sort(); - removeSync('test/_tmp/output'); - - // this should only update the hash of that particular entry point - writeFileSync( - 'test/_tmp/input/main-static.js', - "import {value} from './shared';\nexport default 2 * value;" - ); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - const [newDynamicName, newStaticName, newChunkName] = - readdirSync('test/_tmp/output').sort(); - removeSync('test/_tmp/output'); - assert.notEqual(newStaticName, staticName); - assert.strictEqual(newDynamicName, dynamicName); - assert.strictEqual(newChunkName, chunkName); - staticName = newStaticName; - - // this should update all hashes - writeFileSync('test/_tmp/input/shared.js', 'export const value = 42;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - const [newDynamicName, newStaticName, newChunkName] = - readdirSync('test/_tmp/output').sort(); - assert.notEqual(newStaticName, staticName); - assert.notEqual(newDynamicName, dynamicName); - assert.notEqual(newChunkName, chunkName); - } - ]); + return copy('test/watch/samples/hashing', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: ['test/_tmp/input/main-static.js', 'test/_tmp/input/main-dynamic.js'], + output: { + dir: 'test/_tmp/output', + format: 'cjs', + exports: 'auto', + entryFileNames: '[name].[hash].js', + chunkFileNames: '[name].[hash].js' + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + [dynamicName, staticName, chunkName] = readdirSync('test/_tmp/output').sort(); + removeSync('test/_tmp/output'); + + // this should only update the hash of that particular entry point + writeFileSync( + 'test/_tmp/input/main-static.js', + "import {value} from './shared';\nexport default 2 * value;" + ); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + const [newDynamicName, newStaticName, newChunkName] = + readdirSync('test/_tmp/output').sort(); + removeSync('test/_tmp/output'); + assert.notEqual(newStaticName, staticName); + assert.strictEqual(newDynamicName, dynamicName); + assert.strictEqual(newChunkName, chunkName); + staticName = newStaticName; + + // this should update all hashes + writeFileSync('test/_tmp/input/shared.js', 'export const value = 42;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + const [newDynamicName, newStaticName, newChunkName] = + readdirSync('test/_tmp/output').sort(); + assert.notEqual(newStaticName, staticName); + assert.notEqual(newDynamicName, dynamicName); + assert.notEqual(newChunkName, chunkName); + } + ]); + }); }); it('runs transforms again on previously erroring files that were changed back', () => { @@ -1252,72 +1185,69 @@ describe('rollup.watch', () => { it('skips filesystem writes when configured', () => { let watchChangeCnt = 0; - return sander - .copydir('test/watch/samples/skip-writes') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - watch: { - skipWrite: true - }, - plugins: { - watchChange(id) { - watchChangeCnt++; - assert.strictEqual(id, path.resolve('test/_tmp/input/main.js')); - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - watchChangeCnt = 0; - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - assert.strictEqual(watchChangeCnt, 1); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - assert.strictEqual(watchChangeCnt, 2); - writeFileSync('test/_tmp/input/main.js', 'export default 43;'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - // 'END', - evt => { - assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); - assert.strictEqual(watchChangeCnt, 3); - // still aware of its output destination - assert.strictEqual(evt.output[0], path.resolve('test/_tmp/output/bundle.js')); + return copy('test/watch/samples/skip-writes', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + watch: { + skipWrite: true + }, + plugins: { + watchChange(id) { + watchChangeCnt++; + assert.strictEqual(id, resolve('test/_tmp/input/main.js')); } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + watchChangeCnt = 0; + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(watchChangeCnt, 1); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(watchChangeCnt, 2); + writeFileSync('test/_tmp/input/main.js', 'export default 43;'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + // 'END', + evt => { + assert.strictEqual(existsSync('../_tmp/output/bundle.js'), false); + assert.strictEqual(watchChangeCnt, 3); + // still aware of its output destination + assert.strictEqual(evt.output[0], resolve('test/_tmp/output/bundle.js')); + } + ]); + }); }); it('rebuilds immediately by default', async () => { - await sander.copydir('test/watch/samples/basic').to('test/_tmp/input'); + await copy('test/watch/samples/basic', 'test/_tmp/input'); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', output: { @@ -1355,7 +1285,7 @@ describe('rollup.watch', () => { }); it('observes configured build delays', async () => { - await sander.copydir('test/watch/samples/basic').to('test/_tmp/input'); + await copy('test/watch/samples/basic', 'test/_tmp/input'); watcher = rollup.watch( [ { @@ -1435,356 +1365,333 @@ describe('rollup.watch', () => { describe('addWatchFile', () => { it('supports adding additional watch files in plugin hooks', () => { const watchChangeIds = new Set(); - const buildStartFile = path.resolve('test/_tmp/input/buildStart'); - const loadFile = path.resolve('test/_tmp/input/load'); - const resolveIdFile = path.resolve('test/_tmp/input/resolveId'); - const transformFile = path.resolve('test/_tmp/input/transform'); + const buildStartFile = resolve('test/_tmp/input/buildStart'); + const loadFile = resolve('test/_tmp/input/load'); + const resolveIdFile = resolve('test/_tmp/input/resolveId'); + const transformFile = resolve('test/_tmp/input/transform'); const watchFiles = [buildStartFile, loadFile, resolveIdFile, transformFile]; - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - for (const file of watchFiles) writeFileSync(file, 'initial'); - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + for (const file of watchFiles) writeFileSync(file, 'initial'); + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + buildStart() { + this.addWatchFile(buildStartFile); }, - plugins: { - buildStart() { - this.addWatchFile(buildStartFile); - }, - load() { - this.addWatchFile(loadFile); - }, - resolveId() { - this.addWatchFile(resolveIdFile); - }, - transform() { - this.addWatchFile(transformFile); - }, - watchChange(id) { - watchChangeIds.add(id); - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - // sometimes the watcher is triggered during the initial run - watchChangeIds.clear(); - for (const file of watchFiles) writeFileSync(file, 'changed'); + load() { + this.addWatchFile(loadFile); }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 42); - assert.deepStrictEqual([...watchChangeIds].sort(), watchFiles.sort()); + resolveId() { + this.addWatchFile(resolveIdFile); + }, + transform() { + this.addWatchFile(transformFile); + }, + watchChange(id) { + watchChangeIds.add(id); } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + // sometimes the watcher is triggered during the initial run + watchChangeIds.clear(); + for (const file of watchFiles) writeFileSync(file, 'changed'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 42); + assert.deepStrictEqual([...watchChangeIds].sort(), watchFiles.sort()); + } + ]); + }); }); it('respects changed watched files in the load hook', () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); - return sander - .copydir('test/watch/samples/watch-files') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - plugins: { - load() { - this.addWatchFile(WATCHED_ID); - return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); - writeFileSync(WATCHED_ID, 'next'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 'next'); + const WATCHED_ID = resolve('test/_tmp/input/watched'); + return copy('test/watch/samples/watch-files', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + load() { + this.addWatchFile(WATCHED_ID); + return `export default "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); + writeFileSync(WATCHED_ID, 'next'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 'next'); + } + ]); + }); }); it('respects changed watched files in the transform hook and removes them if they are no longer watched', () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); + const WATCHED_ID = resolve('test/_tmp/input/watched'); let addWatchFile = true; - return sander - .copydir('test/watch/samples/watch-files') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' + return copy('test/watch/samples/watch-files', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + resolveId(id) { + if (id === 'dep') { + return id; + } }, - plugins: { - resolveId(id) { - if (id === 'dep') { - return id; - } - }, - load(id) { - if (id === 'dep') { - return `throw new Error('This should not be executed);`; - } - }, - transform(code, id) { - if (id.endsWith('main.js')) { - return `export { value as default } from 'dep';`; - } else { - if (addWatchFile) { - this.addWatchFile(WATCHED_ID); - } - return `export const value = "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; - } + load(id) { + if (id === 'dep') { + return `throw new Error('This should not be executed);`; } - } - }); - const events = []; - watcher.on('event', event => events.push(event.code)); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); - addWatchFile = false; - writeFileSync(WATCHED_ID, 'next'); }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), 'next'); - writeFileSync(WATCHED_ID, 'other'); - events.length = 0; - return wait(400).then(() => assert.deepStrictEqual(events, [])); + transform(code, id) { + if (id.endsWith('main.js')) { + return `export { value as default } from 'dep';`; + } else { + if (addWatchFile) { + this.addWatchFile(WATCHED_ID); + } + return `export const value = "${readFileSync(WATCHED_ID, 'utf8').trim()}"`; + } } - ]); + } }); + const events = []; + watcher.on('event', event => events.push(event.code)); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 'initial'); + addWatchFile = false; + writeFileSync(WATCHED_ID, 'next'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), 'next'); + writeFileSync(WATCHED_ID, 'other'); + events.length = 0; + return wait(400).then(() => assert.deepStrictEqual(events, [])); + } + ]); + }); }); it('respects changed watched modules that are already part of the graph in the transform hook', () => { - return sander - .copydir('test/watch/samples/dependencies') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - plugins: { - transform(code, id) { - if (id.endsWith('dep1.js')) { - this.addWatchFile(path.resolve('test/_tmp/input/dep2.js')); - const text = readFileSync('test/_tmp/input/dep2.js', 'utf8').trim(); - return `export default ${JSON.stringify(text)}`; - } + return copy('test/watch/samples/dependencies', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + transform(code, id) { + if (id.endsWith('dep1.js')) { + this.addWatchFile(resolve('test/_tmp/input/dep2.js')); + const text = readFileSync('test/_tmp/input/dep2.js', 'utf8').trim(); + return `export default ${JSON.stringify(text)}`; } } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual( - run('../_tmp/output/bundle.js'), - `dep1: "export default 'dep2';", dep2: "dep2"` - ); - writeFileSync('test/_tmp/input/dep2.js', 'export default "next";'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual( - run('../_tmp/output/bundle.js'), - `dep1: "export default "next";", dep2: "next"` - ); - } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual( + run('../_tmp/output/bundle.js'), + `dep1: "export default 'dep2';", dep2: "dep2"` + ); + writeFileSync('test/_tmp/input/dep2.js', 'export default "next";'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual( + run('../_tmp/output/bundle.js'), + `dep1: "export default "next";", dep2: "next"` + ); + } + ]); + }); }); it('respects changed watched directories in the transform hook', () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); - return sander - .copydir('test/watch/samples/watch-files') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - plugins: { - transform() { - this.addWatchFile('test/_tmp/input'); - return `export default ${existsSync(WATCHED_ID)}`; - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), true); - unlinkSync(WATCHED_ID); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), false); - watcher.close(); + const WATCHED_ID = resolve('test/_tmp/input/watched'); + return copy('test/watch/samples/watch-files', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + transform() { + this.addWatchFile('test/_tmp/input'); + return `export default ${existsSync(WATCHED_ID)}`; } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), true); + unlinkSync(WATCHED_ID); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), false); + watcher.close(); + } + ]); + }); }); it('respects initially missing added watched files', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - plugins: { - transform() { - this.addWatchFile('test/_tmp/input/dep'); - return `export default ${existsSync('test/_tmp/input/dep')}`; - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), false); - writeFileSync('test/_tmp/input/dep', ''); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), true); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + transform() { + this.addWatchFile('test/_tmp/input/dep'); + return `export default ${existsSync('test/_tmp/input/dep')}`; } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), false); + writeFileSync('test/_tmp/input/dep', ''); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), true); + } + ]); + }); }); it('respects unlinked and re-added watched files', () => { - return sander - .copydir('test/watch/samples/basic') - .to('test/_tmp/input') - .then(() => { - writeFileSync('test/_tmp/input/dep', ''); - watcher = rollup.watch({ - input: 'test/_tmp/input/main.js', - output: { - file: 'test/_tmp/output/bundle.js', - format: 'cjs', - exports: 'auto' - }, - plugins: { - transform() { - this.addWatchFile('test/_tmp/input/dep'); - return `export default ${existsSync('test/_tmp/input/dep')}`; - } - } - }); - - return sequence(watcher, [ - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), true); - unlinkSync('test/_tmp/input/dep'); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), false); - writeFileSync('test/_tmp/input/dep', ''); - }, - 'START', - 'BUNDLE_START', - 'BUNDLE_END', - 'END', - () => { - assert.strictEqual(run('../_tmp/output/bundle.js'), true); + return copy('test/watch/samples/basic', 'test/_tmp/input').then(() => { + writeFileSync('test/_tmp/input/dep', ''); + watcher = rollup.watch({ + input: 'test/_tmp/input/main.js', + output: { + file: 'test/_tmp/output/bundle.js', + format: 'cjs', + exports: 'auto' + }, + plugins: { + transform() { + this.addWatchFile('test/_tmp/input/dep'); + return `export default ${existsSync('test/_tmp/input/dep')}`; } - ]); + } }); + + return sequence(watcher, [ + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), true); + unlinkSync('test/_tmp/input/dep'); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), false); + writeFileSync('test/_tmp/input/dep', ''); + }, + 'START', + 'BUNDLE_START', + 'BUNDLE_END', + 'END', + () => { + assert.strictEqual(run('../_tmp/output/bundle.js'), true); + } + ]); + }); }); it('does not rerun the transform hook if a non-watched change triggered the re-run', () => { - const WATCHED_ID = path.resolve('test/_tmp/input/watched'); + const WATCHED_ID = resolve('test/_tmp/input/watched'); let transformRuns = 0; - return sander - .copydir('test/watch/samples/watch-files') - .to('test/_tmp/input') + return copy('test/watch/samples/watch-files', 'test/_tmp/input') .then(() => wait(100)) .then(() => { writeFileSync('test/_tmp/input/alsoWatched', 'initial'); From cd505fcf9773469738ef4f44498bd5e6c7ff3f8f Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 22 Jan 2022 22:41:33 -0500 Subject: [PATCH 8/9] remove sander --- package-lock.json | 36 +++--------------------------------- package.json | 1 - 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2aead112cd3..e82e457cf88 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3127,7 +3127,7 @@ "log-symbols": "4.1.0", "minimatch": "3.0.4", "ms": "2.1.3", - "nanoid": "^3.1.31", + "nanoid": "3.1.25", "serialize-javascript": "6.0.0", "strip-json-comments": "3.1.1", "supports-color": "8.1.1", @@ -3278,7 +3278,8 @@ "nanoid": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.2.0.tgz", - "integrity": "sha512-fmsZYa9lpn69Ad5eDn7FMcnnSR+8R34W9qJEijxYhTbfOWzr22n1QxCMzXLK+ODyW2973V3Fux959iQoUxzUIA==" + "integrity": "sha512-fmsZYa9lpn69Ad5eDn7FMcnnSR+8R34W9qJEijxYhTbfOWzr22n1QxCMzXLK+ODyW2973V3Fux959iQoUxzUIA==", + "dev": true }, "natural-compare": { "version": "1.4.0", @@ -4111,37 +4112,6 @@ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", "dev": true }, - "sander": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/sander/-/sander-0.6.0.tgz", - "integrity": "sha1-rxYkzX+2362Y6+9WUxn5IAeNqSU=", - "dev": true, - "requires": { - "graceful-fs": "^4.1.3", - "mkdirp": "^0.5.1", - "rimraf": "^2.5.2" - }, - "dependencies": { - "mkdirp": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", - "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", - "dev": true, - "requires": { - "minimist": "^1.2.5" - } - }, - "rimraf": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", - "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", - "dev": true, - "requires": { - "glob": "^7.1.3" - } - } - } - }, "semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", diff --git a/package.json b/package.json index b2ec7ae6e61..ae7ac15141c 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,6 @@ "rollup-plugin-string": "^3.0.0", "rollup-plugin-terser": "^7.0.2", "rollup-plugin-thatworks": "^1.0.4", - "sander": "^0.6.0", "shx": "^0.3.4", "signal-exit": "^3.0.6", "source-map": "^0.7.3", From c637a8629dd0caca183aec0396eb596b561a6d39 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Fri, 28 Jan 2022 20:58:15 -0500 Subject: [PATCH 9/9] fix test --- test/watch/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/watch/index.js b/test/watch/index.js index c20cee3d8b1..648f3e819b7 100644 --- a/test/watch/index.js +++ b/test/watch/index.js @@ -1253,6 +1253,7 @@ describe('rollup.watch', () => { it('rebuilds immediately by default', async () => { await copy('test/watch/samples/basic', 'test/_tmp/input'); + await wait(100); watcher = rollup.watch({ input: 'test/_tmp/input/main.js', output: {