Skip to content

Commit

Permalink
Use commonjs in rollup config for __dirname
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Sep 23, 2020
1 parent 4b513cb commit 90c060b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
36 changes: 22 additions & 14 deletions build/rollup-common-config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import path from 'path';
import fs from 'fs';
import json from '@rollup/plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import typescript from 'rollup-plugin-typescript2';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import replace from '@rollup/plugin-replace';
const path = require('path');
const fs = require('fs');
const json = require('@rollup/plugin-json');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('rollup-plugin-typescript2');
const resolve = require('@rollup/plugin-node-resolve').default;
const { terser } = require('rollup-plugin-terser');
const replace = require('@rollup/plugin-replace');

export const production = process.env.MINIFY === 'false' ? false : !process.env.ROLLUP_WATCH;
const production = process.env.MINIFY === 'false' ? false : !process.env.ROLLUP_WATCH;

export const getRootURL = root => relative => path.join(root, relative);
const getRootURL = root => relative => path.join(root, relative);

export const clearDist = dist => {
const clearDist = dist => {
if (fs.existsSync(dist)) {
fs.rmdirSync(dist, { recursive: true });
}
};

export const onwarn = (warning, warn) => {
const onwarn = (warning, warn) => {
// typescript tslib
if (warning.code === 'THIS_IS_UNDEFINED') return;
// ignorePartSourcemap
Expand All @@ -31,7 +31,7 @@ export const onwarn = (warning, warn) => {
// console.log(warning)
};

export const external = [
const external = [
// node built-in
'path',
'fs',
Expand All @@ -58,7 +58,7 @@ export const external = [
'stylus'
];

export const createPlugins = tsconfig => [
const createPlugins = tsconfig => [
json(),
resolve({ dedupe: ['typescript', 'debug'], preferBuiltins: true }),
commonjs({
Expand Down Expand Up @@ -89,3 +89,11 @@ export const createPlugins = tsconfig => [
typescript({ tsconfig, tsconfigOverride: { compilerOptions: { module: 'esnext' } } }),
production && terser()
];

module.exports = {
getRootURL,
clearDist,
onwarn,
external,
createPlugins
};
6 changes: 4 additions & 2 deletions build/rollup-plugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function linkVlsInCLI() {
function linkVlsInCLI() {
return {
name: 'link-vls-in-cli',
resolveId(source, importer) {
Expand All @@ -10,7 +10,7 @@ export function linkVlsInCLI() {
};
}

export function ignorePartSourcemap() {
function ignorePartSourcemap() {
return {
name: 'ignore-part-sourcemap',
transform(code, id) {
Expand All @@ -26,3 +26,5 @@ export function ignorePartSourcemap() {
}
};
}

module.exports = { linkVlsInCLI, ignorePartSourcemap };
6 changes: 3 additions & 3 deletions client/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getRootURL, clearDist, external, createPlugins } from '../build/rollup-common-config';
import clientPkg from '../package.json';
const { getRootURL, clearDist, external, createPlugins } = require('../build/rollup-common-config');
const clientPkg = require('../package.json');

const getClientURL = getRootURL('client');

clearDist(getClientURL('../dist'));
export default {
module.exports = {
input: getClientURL('vueMain.ts'),
output: { file: clientPkg.main, name: clientPkg.name, format: 'cjs', sourcemap: true },
external,
Expand Down
10 changes: 5 additions & 5 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ClientConfig from './client/rollup.config.js';
import VlsConfigs from './server/rollup.config.js';
import VtiConfig from './vti/rollup.config.js';

export default [ClientConfig, ...VlsConfigs, VtiConfig];
module.exports = [
require('./client/rollup.config.js'),
...require('./server/rollup.config.js'),
require('./vti/rollup.config.js')
];
16 changes: 8 additions & 8 deletions server/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs-extra';
import path from 'path';
import fg from 'fast-glob';
import { getRootURL, clearDist, external, onwarn, createPlugins } from '../build/rollup-common-config';
import { linkVlsInCLI, ignorePartSourcemap } from '../build/rollup-plugins.js';
import vlsPkg from './package.json';
const fs = require('fs-extra');
const path = require('path');
const fg = require('fast-glob');
const { getRootURL, clearDist, external, onwarn, createPlugins } = require('../build/rollup-common-config');
const { linkVlsInCLI, ignorePartSourcemap } = require('../build/rollup-plugins.js');
const vlsPkg = require('./package.json');

const getVlsURL = getRootURL('server');

Expand All @@ -16,7 +16,7 @@ function copySnippets() {
fs.copySync(
path.resolve(__dirname, '../', getVlsURL('src/modes/vue/veturSnippets')),
path.resolve(__dirname, '../', getVlsURL('dist/veturSnippets')),
{ overwrite: true }
{ overwrite: true, recursive: true }
);
}
};
Expand All @@ -34,7 +34,7 @@ function copyTSDefaultLibs() {
};
}

export default [
module.exports = [
// vls
{
input: getVlsURL('src/main.ts'),
Expand Down
6 changes: 3 additions & 3 deletions vti/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getRootURL, clearDist, external, onwarn, createPlugins } from '../build/rollup-common-config';
import vtiPkg from './package.json';
const { getRootURL, clearDist, external, onwarn, createPlugins } = require('../build/rollup-common-config');
const vtiPkg = require('./package.json');

const getVtiURL = getRootURL('vti');

clearDist(getVtiURL('dist'));
export default {
module.exports = {
input: getVtiURL('src/cli.ts'),
output: { file: getVtiURL(vtiPkg.main), name: vtiPkg.name, format: 'cjs', sourcemap: true },
external: [...external, 'vls'],
Expand Down

0 comments on commit 90c060b

Please sign in to comment.